MySQL:提取唯一日期
MySQL: extract unique dates
我用
创建了一个table
CREATE TABLE visits (
user_id int,
event_date timestamp
);
INSERT INTO visits (user_id, event_date)
VALUES
(1, '2021-12-22 12:12:00'),
(1, '2021-12-23 12:12:05'),
(1, '2021-12-24 12:13:00'),
(1, '2021-12-24 12:14:00'),
(1, '2022-03-10 12:14:00'),
(1, '2022-03-11 12:14:00'),
(2, '2021-12-23 12:12:00'),
(1, '2022-03-12 12:14:00'),
(2, '2021-12-23 13:12:00'),
(1, '2022-03-13 12:14:00'),
(1, '2022-03-14 12:14:00'),
(3, '2021-12-25 12:12:00'),
(1, '2022-03-15 12:14:00'),
(1, '2022-03-20 12:14:00'),
(1, '2022-03-21 12:14:00'),
(1, '2022-03-23 12:14:00'),
(1, '2022-03-24 12:14:00'),
(1, '2022-03-25 12:14:00'),
(3, '2021-12-30 12:12:00'),
(3, '2021-12-31 12:12:00'),
(3, '2021-12-31 12:12:00'),
(4, '2022-03-21 12:12:00'),
(4, '2022-03-22 12:12:00'),
(4, '2022-03-23 12:12:00'),
(4, '2022-03-24 12:12:00');
然后我尝试使用
提取独特的日期
select
user_id,
distinct cast(event_date as date) as event_date
from visits;
然后我得到
ERROR 1064 (42000) at line 111: 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 'distinct cast(event_date as date) as event_date
来自第 3 行的访问
我做错了什么?
您应该将关键字 distinct 放在第一个位置:
select distinct cast(event_date as date) as event_date, user_id
from visits;
或
select distinct user_id, cast(event_date as date) as event_date
from visits;
您可以使用 GROUP BY
。
(COUNT
列是可选的)
SELECT
count(*) "number",
user_id,
DATE(event_date) "event date"
FROM visits
GROUP BY
user_id,
DATE(event_date)
ORDER BY
user_id,
DATE(event_date);
我用
创建了一个tableCREATE TABLE visits (
user_id int,
event_date timestamp
);
INSERT INTO visits (user_id, event_date)
VALUES
(1, '2021-12-22 12:12:00'),
(1, '2021-12-23 12:12:05'),
(1, '2021-12-24 12:13:00'),
(1, '2021-12-24 12:14:00'),
(1, '2022-03-10 12:14:00'),
(1, '2022-03-11 12:14:00'),
(2, '2021-12-23 12:12:00'),
(1, '2022-03-12 12:14:00'),
(2, '2021-12-23 13:12:00'),
(1, '2022-03-13 12:14:00'),
(1, '2022-03-14 12:14:00'),
(3, '2021-12-25 12:12:00'),
(1, '2022-03-15 12:14:00'),
(1, '2022-03-20 12:14:00'),
(1, '2022-03-21 12:14:00'),
(1, '2022-03-23 12:14:00'),
(1, '2022-03-24 12:14:00'),
(1, '2022-03-25 12:14:00'),
(3, '2021-12-30 12:12:00'),
(3, '2021-12-31 12:12:00'),
(3, '2021-12-31 12:12:00'),
(4, '2022-03-21 12:12:00'),
(4, '2022-03-22 12:12:00'),
(4, '2022-03-23 12:12:00'),
(4, '2022-03-24 12:12:00');
然后我尝试使用
提取独特的日期select
user_id,
distinct cast(event_date as date) as event_date
from visits;
然后我得到
ERROR 1064 (42000) at line 111: 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 'distinct cast(event_date as date) as event_date
来自第 3 行的访问
我做错了什么?
您应该将关键字 distinct 放在第一个位置:
select distinct cast(event_date as date) as event_date, user_id
from visits;
或
select distinct user_id, cast(event_date as date) as event_date
from visits;
您可以使用 GROUP BY
。
(COUNT
列是可选的)
SELECT
count(*) "number",
user_id,
DATE(event_date) "event date"
FROM visits
GROUP BY
user_id,
DATE(event_date)
ORDER BY
user_id,
DATE(event_date);