为我的查询选择不同的子字符串帮助[关闭]

问题描述 投票:0回答:1
SELECT 
DISTINCT(date(substring(chat_date,0,10))) 
AS chat_date, id, 
count(*) 
AS count 
FROM chat 
GROUP BY chat_date 
ORDER BY id ASC

该列的格式为chat_date:YYYY-mm-dd hh:ii:ss我应该使查询只读YYYY-mm-dd

mysql select distinct
1个回答
1
投票
create table chat
(   id int auto_increment primary key,
    chatId int not null,    -- MAIN CHAT THREAD ID
    chatUser int not null,
    chat_date datetime not null,
    saidWhat varchar(255) not null
);
truncate table chat;
insert chat(chatId,chatUser,chat_date,saidWhat) values (7,101,now(),'hello');
insert chat(chatId,chatUser,chat_date,saidWhat) values (7,42,now(),'hi right back at you');
insert chat(chatId,chatUser,chat_date,saidWhat) values (7,101,now(),'can you help me i am lost');
insert chat(chatId,chatUser,chat_date,saidWhat) values (7,666,now(),'ask Siri');

insert chat(chatId,chatUser,chat_date,saidWhat) values (8,67,now(),'Hi, I over-eat');
insert chat(chatId,chatUser,chat_date,saidWhat) values (8,58,now(),'me too! I like Doritos');
insert chat(chatId,chatUser,chat_date,saidWhat) values (8,8001,now(),'Try rice cakes');

select min(chat_date),max(chat_date) from chat;
+---------------------+---------------------+
| min(chat_date)      | max(chat_date)      |
+---------------------+---------------------+
| 2015-08-09 20:32:12 | 2015-08-09 20:32:12 |
+---------------------+---------------------+

SELECT date(chat_date) as chat_date, chatId, count(*) as theCount
FROM chat 
GROUP BY date(chat_date),chatId 
ORDER BY chatId
+------------+--------+----------+
| chat_date  | chatId | theCount |
+------------+--------+----------+
| 2015-08-09 |      7 |        4 |
| 2015-08-09 |      8 |        3 |
+------------+--------+----------+
© www.soinside.com 2019 - 2024. All rights reserved.