按日期搜索mysql

时间:2016-05-28 14:49:14

标签: mysql datetime search

我有一个名为messages的表:

date (format DATETIME)
id
text
user_id(FK)

以及名为users的表格:

id
name

如何获取每个用户的给定日期(message date = xxxx-xx-xx)的消息数? 像:

 user       message_count
 user1_name      5
 user2_name      2

今天如何让用户收到3条或更多邮件?

SELECT users.name 
FROM users 
WHERE Users.id IN ( SELECT Messages.user_id 
                    FROM chat.Messages 
                    GROUP BY Messages.user_id 
                    HAVING COUNT(*) >=3
                  )

它会返回有3条或更多邮件的用户,如何获取用户,今天有3条或更多邮件的用户? where date = now()

2 个答案:

答案 0 :(得分:0)

select b.name,count(*) from messages a,users b where date ='2016-05-28' group by b.name

答案 1 :(得分:0)

如何为每个用户获取给定日期(消息日期= xxxx-xx-xx)的消息计数?

查询#1:

SELECT 
users.name,
COUNT(*) message_count
FROM messages 
INNER JOIN users ON messages.user_id = users.id
WHERE messages.date = PUT_YOUR_DATE_HERE
GROUP BY messages.user_id;

如何获取今天有3条或更多邮件的用户的邮件数量?

查询#2:

SELECT 
users.name,
COUNT(*) message_count
FROM messages 
INNER JOIN users ON messages.user_id = users.id
WHERE messages.date = CURDATE()
GROUP BY messages.user_id
HAVING COUNT(*) >= 3;