mysql - SQL 中的保留分析

标签 mysql sql

我正在尝试分析首次购买后 30 天内购买的用户。我不断得到

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':: date) - integer '30' AND i.customer_id = o.customer_id group by o.custome' at line 7

这是我运行的代码。

select 
o.customer_id, 
o.purchase_time
from orders as o
join orders as i on 
i.purchase_time <= o.purchase_time AND
i.purchase_time >= (o.purchase_time :: date) - integer '30' AND
i.customer_id = o.customer_id
group by o.customer_id, o.purchase_time;

最佳答案

我怀疑这就是你想要的,MySQL 语法:

select o.*
from orders o
where exists (select 1
              from orders o2
              where o2.customer_id = o.customer_id and
                    o2.purchase_time > o.purchase_time and
                    o2.purchase_time <= purchase_time + interval 30 day
            );

关于mysql - SQL 中的保留分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55738204/

相关文章:

mysql - 选择相同条件存在 2 个不同值的 mysql 行

mysql - 规范化和规范形式 : database

sql - 连接表时如何显示未使用的外键值

java - 用户输入的 SQL 注入(inject)问题

c# - SQL Server 检查存储过程是否包含 WITH(NOLOCK)

mysql - Sequelize : Problem with dobule $ in JSON_EXTRACT

mysql - DBeaver:以毫秒显示日期时间

php - 如何使用 PHP 将文本文件(CSV)解析到 MySQL 中

mysql - 我应该如何更改我的查询?

mysql - 我的SQL 5.6-如何防止读取前一个SQL语句选择的行以及在给定列中共享重复值的任何其他行