为我的查询选择distinct substring help

时间:2015-08-09 19:25:57

标签: php mysql select distinct

$res=mysql_query("
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

1 个答案:

答案 0 :(得分: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 |
+------------+--------+----------+