Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.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
Php MySQL:两条记录对一条记录_Php_Mysql_Laravel - Fatal编程技术网

Php MySQL:两条记录对一条记录

Php MySQL:两条记录对一条记录,php,mysql,laravel,Php,Mysql,Laravel,我有一张这样的桌子 id conversation_id messageable_id 1 1 3 <-- 2 1 5 3 2 7 4 2 3 <-- 5 3 7 6 3

我有一张这样的桌子

id conversation_id messageable_id
 1               1              3 <--
 2               1              5
 3               2              7
 4               2              3 <--
 5               3              7
 6               3              9
如何生成查询

提前感谢。

您可以使用MySQL 8.0中提供的lead:

select *
from (
    select
        conversation_id,
        messageable_id sender_id,
        lead(messageable_id) over(partition by conversation_id order by id) receiver_id
    from mytable
) t
where sender_id = 3
select *
from (
    select
        conversation_id,
        messageable_id sender_id,
        lead(messageable_id) over(partition by conversation_id order by id) receiver_id
    from mytable
) t
where sender_id = 3