Выберите отдельную подстроку для моего запроса

$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: ГГГГ-мм-дд чч: ii: сс Я должен сделать запрос только для чтения ГГГГ-мм-дд

0

Решение

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 |
+------------+--------+----------+
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector