SQL组合组合?

时间:2011-02-28 05:30:41

标签: mysql group-by

我在从表中选择项目时遇到问题,其中device_id可以位于from_device_id列或to_device_id列中。我试图返回给定设备ID的所有聊天在from_device_id或to_device_id列中,但只返回最新消息。

 select chat.*, (select screen_name from usr where chat.from_device_id=usr.device_id limit 1) as from_screen_name, (select screen_name from usr where chat.to_device_id=usr.device_id limit 1) as to_screen_name from chat where to_device_id="ffffffff-af28-3427-a2bc-83865900edbe" or from_device_id="ffffffff-af28-3427-a2bc-83865900edbe" group by from_device_id, to_device_id;

+----+--------------------------------------+--------------------------------------+---------+---------------------+------------------+----------------+
| id | from_device_id                       | to_device_id                         | message | date                | from_screen_name | to_screen_name |
+----+--------------------------------------+--------------------------------------+---------+---------------------+------------------+----------------+
| 20 | ffffffff-af28-3427-a2bc-83860033c587 | ffffffff-af28-3427-a2bc-83865900edbe | ee      | 2011-02-28 12:36:38 | kevin            | handset        |
|  1 | ffffffff-af28-3427-a2bc-83865900edbe | ffffffff-af28-3427-a2bc-83860033c587 | yyy     | 2011-02-27 17:43:17 | handset          | kevin          |
+----+--------------------------------------+--------------------------------------+---------+---------------------+------------------+----------------+
2 rows in set (0.00 sec)

正如预期的那样,返回两行。如何修改此查询以仅返回一行?

mysql> describe chat;
+----------------+---------------+------+-----+-------------------+----------------+
| Field          | Type          | Null | Key | Default           | Extra          |
+----------------+---------------+------+-----+-------------------+----------------+
| id             | int(11)       | NO   | PRI | NULL              | auto_increment |
| from_device_id | varchar(128)  | NO   |     | NULL              |                |
| to_device_id   | varchar(128)  | NO   |     | NULL              |                |
| message        | varchar(2048) | NO   |     | NULL              |                |
| date           | timestamp     | YES  |     | CURRENT_TIMESTAMP |                |
+----------------+---------------+------+-----+-------------------+----------------+
5 rows in set (0.00 sec)

2 个答案:

答案 0 :(得分:2)

select chat.*, 
       (select screen_name 
        from usr 
        where chat.from_device_id=usr.device_id
        limit 1
       ) as from_screen_name, 
       (select screen_name 
        from usr 
        where chat.to_device_id=usr.device_id
        limit 1
       ) as to_screen_name
from chat 
where to_device_id="ffffffff-af28-3427-a2bc-83865900edbe" or 
      from_device_id="ffffffff-af28-3427-a2bc-83865900edbe" 
group by from_device_id, to_device_id
order by date DESC
limit 1;

您需要告诉SQL它应该按日期对返回的数据进行排序以获取最近的聊天。然后,您只需将返回的行限制为1。

答案 1 :(得分:2)

您根本不需要使用Group By。相反,您只需使用Limit谓词返回最后一行。此外,您不应该使用子查询,因为您可以使用简单的连接。如果chat.from_device_idchat.to_device_id都不可为空,那么您可以将内部联接替换为左连接。

Select chat.id
    , chat.from_device_id
    , chat.to_device_id
    , chat.message
    , chat.date
    , FromUser.screen_name As from_screen_nam
    , ToUser.screen_name As to_screen_name 
From chat
    Left Join usr As FromUser
        On FromUser.device_id = chat.from_device_id
    Left Join usr As ToUser
        On ToUser.device_id = chat.to_device_id
Where chat.to_device_id="ffffffff-af28-3427-a2bc-83865900edbe" 
    Or chat.from_device_id="ffffffff-af28-3427-a2bc-83865900edbe" 
Order By chat.date Desc
Limit 1