Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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
如果from id和to id值在任何列中匹配,Mysql将显示一个列(id最高的列)_Mysql - Fatal编程技术网

如果from id和to id值在任何列中匹配,Mysql将显示一个列(id最高的列)

如果from id和to id值在任何列中匹配,Mysql将显示一个列(id最高的列),mysql,Mysql,如何使用max显示组中id最高的记录 也许一个例子会更清楚。给定一个名为messages的表,我有5列id,从\u id到\u id,text,在以下位置创建: id: id of the message from_id: user id of message sender to_id: user id of message receiver text: the message body created_at: the time message was sent ---+---------

如何使用max显示组中id最高的记录

也许一个例子会更清楚。给定一个名为messages的表,我有5列id,从\u id到\u id,text,在以下位置创建:


id: id of the message
from_id: user id of message sender 
to_id: user id of message receiver
text: the message body
created_at: the time message was sent

---+---------+--------+----------+----------+
id | from_id | to_id  |text      |created_at|
---+---------+--------+----------+----------+
1  | 1       | 2      |hello     |00000001  |
2  | 2       | 1      |hi        |00000002  |
3  | 1       | 2      |test      |00000003  |
4  | 1       | 2      |checkmg   |00000004  |
5  | 4       | 3      |you went  |00000005  |
6  | 3       | 4      |i did     |00000006  |
7  | 3       | 4      |message   |00000007  |
8  | 3       | 5      |chat tt   |00000008  |
9  | 5       | 3      |chat ts   |00000009  |
10 | 3       | 5      |test ch   |00000010  |
---+---------+--------+----------+----------+
如果多个用户多次交换消息,并且我只想为每个用户显示一行,一行id最高,比较所有“from_id”和“to_id”行,并确保从用户1到用户2以及从用户2到用户1应分组并仅显示一行id号最高。我希望我的答案看起来像下面的结果

---+---------+--------+----------+----------+
id | from_id | to_id  |text      |created_at|
---+---------+--------+----------+----------+
4  | 1       | 2      |checkmg   |00000004  |
7  | 3       | 4      |message   |00000007  |
10 | 3       | 5      |test ch   |00000010  |
---+---------+--------+----------+----------+

查询不适合更改名称,因为使用了保留字,时间戳也是保留字

SELECT m.`id`,m.`from`,m.`to`,m.`text`,m.`timestamp`
from mytable13 m
inner join (
    select 
       max(id) as id 
    from mytable13 
    group by CASE WHEN `from`<`to` THEN `from`*1000+`to` ELSE `to`*1000+`from` END) x
            on x.id=m.id;
表是这样创建的:

CREATE TABLE `mytable13` (
  `id` int DEFAULT NULL,
  `from` int DEFAULT NULL,
  `to` int DEFAULT NULL,
  `text` varchar(20) DEFAULT NULL,
  `timestamp` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
使用以下数据:

insert into mytable13 values 
(1  , 1       , 2      ,'hello'     ,'00000001'  ),
(2  , 2       , 1      ,'hi'        ,'00000002'  ),
(3  , 1       , 2      ,'test'      ,'00000003'  ),
(4  , 1       , 2      ,'checkmg'   ,'00000004'  ),
(5  , 4       , 3      ,'you went'  ,'00000005'  ),
(6  , 3       , 4      ,'i did'     ,'00000006'  ),
(7  , 3       , 4      ,'message'   ,'00000007'  ),
(8  , 3       , 5      ,'chat tt'   ,'00000008'  ),
(9  , 5       , 3      ,'chat ts'   ,'00000009'  ),
(10 , 3       , 5      ,'test ch'   ,'00000010'  );

你不应该在字段列表中使用as name。这只是为了用尽可能短的单词显示示例,如果有必要,我通常使用backtick。我的字段列表不是这样命名的。然后你写了一个糟糕的示例,backtick不是我所说的最新版本is 8.0.23的借口,你的意思可能不是重复上述问题;这个问题隐含的一部分是,首先,如何进行分组
insert into mytable13 values 
(1  , 1       , 2      ,'hello'     ,'00000001'  ),
(2  , 2       , 1      ,'hi'        ,'00000002'  ),
(3  , 1       , 2      ,'test'      ,'00000003'  ),
(4  , 1       , 2      ,'checkmg'   ,'00000004'  ),
(5  , 4       , 3      ,'you went'  ,'00000005'  ),
(6  , 3       , 4      ,'i did'     ,'00000006'  ),
(7  , 3       , 4      ,'message'   ,'00000007'  ),
(8  , 3       , 5      ,'chat tt'   ,'00000008'  ),
(9  , 5       , 3      ,'chat ts'   ,'00000009'  ),
(10 , 3       , 5      ,'test ch'   ,'00000010'  );
select m.*
from (
    select max(id) id
    from messages
    group by least(from_id,to_id),greatest(from_id,to_id)
) ids
natural join messages m;