Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL复杂查询需要建议_Mysql_Group By_Sql Order By - Fatal编程技术网

MySQL复杂查询需要建议

MySQL复杂查询需要建议,mysql,group-by,sql-order-by,Mysql,Group By,Sql Order By,我的表格如下: id | sender | receiver | time 1 | felix@a.com | lea@a.com | 2014-07-04 22:50:16 2 | felix@a.com | lea@a.com | 2014-07-04 22:51:20 3 | felix@a.com | mia@a.com | 2014-07-04 22:51:41 4 | mia@a.com | felix@a.

我的表格如下:

 id | sender      | receiver    | time 
  1 | felix@a.com | lea@a.com   | 2014-07-04 22:50:16    
  2 | felix@a.com | lea@a.com   | 2014-07-04 22:51:20    
  3 | felix@a.com | mia@a.com   | 2014-07-04 22:51:41    
  4 | mia@a.com   | felix@a.com | 2014-07-04 22:52:45    
  5 | mia@a.com   | felix@a.com | 2014-07-04 22:52:58
  6 | lea@a.com   | felix@a.com | 2014-07-04 22:53:33
  7 | felix@a.com | mia@a.com   | 2014-07-04 22:55:53
我想得到一个条目为'felix@a.com'按时间顺序描述。因此,输出如下所示

 id | sender      | receiver    | time
  6 | lea@a.com   | felix@a.com | 2014-07-04 22:53:33
  7 | felix@a.com | mia@a.com   | 2014-07-04 22:55:53
我正在尝试这个,但没有正常工作

SELECT * 
FROM (
SELECT * 
FROM  `message` 
ORDER BY  `time` DESC
) AS  `message` 
WHERE (
message.sender =  'felix@a.com'
OR message.receiver =  'felix@a.com'
)
GROUP BY message.receiver, message.sender
请指导我实现目标的正确方法

SELECT DISTINCT sender, receiver
FROM tablename ORDER BY time DESC;

这个SQL DISTINCT子句示例将返回每个唯一的发送方和接收方组合。在这种情况下,DISTINCT适用于DISTINCT关键字后面列出的每个字段。

谢谢,但这并没有给出我想要的输出,所以您想要sender=felix的最近一行和receiver为felix的最近一行?你可以用UNION来做这件事。谢谢@草莓,它只对上面的输入有效。当他死的时候发生了什么felix@a.com“向许多用户发送或从许多用户接收信息?如果问题发生变化,那么答案也可能会改变。”。
DROP TABLE my_table;

CREATE TABLE my_table
(id INT NOT NULL PRIMARY KEY
,sender VARCHAR(20) NOT NULL     
,receiver VARCHAR(20) NOT NULL    
,time DATETIME NOT NULL
);

INSERT INTO my_table VALUES
(1,'felix@a.com','lea@a.com','2014-07-04 22:50:16'),
(2,'felix@a.com','lea@a.com','2014-07-04 22:51:20'),
(3,'felix@a.com','mia@a.com','2014-07-04 22:51:41'),
(4,'mia@a.com','felix@a.com','2014-07-04 22:52:45'),
(5,'mia@a.com','felix@a.com','2014-07-04 22:52:58'),
(6,'lea@a.com','felix@a.com','2014-07-04 22:53:33'),
(7,'felix@a.com','mia@a.com','2014-07-04 22:55:53');

(SELECT * FROM my_table WHERE sender = 'felix@a.com' ORDER BY time DESC LIMIT 1)
UNION
(SELECT * FROM my_table WHERE receiver = 'felix@a.com' ORDER BY time DESC LIMIT 1);
+----+-------------+-------------+---------------------+
| id | sender      | receiver    | time                |
+----+-------------+-------------+---------------------+
|  7 | felix@a.com | mia@a.com   | 2014-07-04 22:55:53 |
|  6 | lea@a.com   | felix@a.com | 2014-07-04 22:53:33 |
+----+-------------+-------------+---------------------+