简体   繁体   English

从表格中选择“与众不同”并按日期排序

[英]Select distinct from the table and sort by date

I am trying to built the messaging system and I want to select distinct value from the table and sort by date and also return the read and unread status. 我正在尝试构建消息传递系统,我想从表中选择不同的值并按日期排序,还返回已读和未读状态。 My table structure is as id, from_user_id, to_user_id, message, datetime, read . 我的表结构为id, from_user_id, to_user_id, message, datetime, read Suppose dummy data are as follows: 假设伪数据如下:

id   |   from_user_id  | to_user_id   |  message  |      datetime       | read
1    |          20     |     50       |    hi     |  2016-3-13 06:05:30 | 1
2    |          20     |     50       |    hey     |  2016-3-13 06:15:30 | 0
3    |          30     |     50       |    hi     |  2016-3-14 06:05:30 | 0

Now I want to select distinct from_user_id and sort by date so that I can display the name of the user who is chating recently on top of list and also return the read status 1, if any of the rows with from_user_id has read status 0, then I want to return read as 0 so that I can differentiate whose user message has unread message. 现在,我想选择不同的from_user_id并按日期排序,以便我可以在列表顶部显示最近from_user_id的用户的名称,并返回读取状态1(如果任何from_user_id的行的读取状态为0,那么我想将read返回为0,以便区分谁的用户消息为unread消息。

Suppose, from_user_id has read status as 0 in one rows then I want to return read status as 0 when returning distinct value of from_user_id . 假设from_user_idfrom_user_id中的read状态为0,那么我想在返回from_user_id不同值时将read状态返回为0。 How can I do it. 我该怎么做。 I tried as follows but won't work in my case. 我尝试了以下方法,但在我的情况下不起作用。 It returns distinct value but not sorted by date and also read status in not returning what I expect. 它返回不同的值,但不按日期排序,并且在不返回我期望的状态时读取状态。

 select distinct(`from_user_id`),register.name FROM message INNER JOIN register ON register.id = message.from_user_id where message.to_user_id = $user_id GROUP BY message.id ORDER BY MAX(datetime) ASC

Try the following query: 请尝试以下查询:

SELECT
message.from_user_id,
register.name,
message.read
FROM message 
INNER JOIN 
(
  SELECT  
   from_user_id,
   MAX(datetime) max_datetime
  FROM message 
  WHERE to_user_id = 50
  GROUP BY from_user_id ) t
 ON t.from_user_id = message.from_user_id AND t.max_datetime = message.datetime
INNER JOIN register ON register.id = message.from_user_id
WHERE message.to_user_id = 50
ORDER BY message.datetime ASC

UPDATED SQL FIDDLE 更新的SQL字段

Sample Input: 输入样例:

id   |   from_user_id  | to_user_id   |  message  |      datetime        | read

1    |          20     |     50       |    hi     |  2016-3-13 06:05:30  | 1
2    |          20     |     50       |    hey    |  2016-3-13 06:15:30  | 0
3    |          30     |     50       |    hi     |  2016-3-14 06:05:30  | 0

Output: 输出:

from_user_id        name        read

20                 User20      0
30                 User30      0

Note: This query might give you multiple row for the same from_user_id if there exists multiple entries in your message table having the same datetime and same to_user_id . 注意:如果message表中存在多个具有相同datetime和相同to_user_id条目,则此查询可能为您提供同一from_user_id多行。

checkout this query : 签出此查询:

SELECT DISTINCT(form_user_id),MIN(read) FROM table_name ORDER BY datetime ASC SELECT DISTINCT(form_user_id),MIN(read)from table_name ORDER BY datetime ASC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM