当前位置:Gxlcms > PHP教程 > 这样的sql语句怎么写

这样的sql语句怎么写

时间:2021-07-01 10:21:17 帮助过:6人阅读

一个礼物表: from(赠送者) to(收到者)
一个用户表:id nickname

from和to都关联用户表的id,怎么查询出结果:

赠送者昵称 收到者昵称

回复内容:

一个礼物表: from(赠送者) to(收到者)
一个用户表:id nickname

from和to都关联用户表的id,怎么查询出结果:

赠送者昵称 收到者昵称

select *
from 礼物
left join 用户 as 发送方
on 礼物.from =发送方.id
left join 用户 as 接受方
on 礼物.to = 接收方.id

其中的中文换成对应英文。。。字段自己处理一下

多对多的表连接查询

select usertable1.nickname, usertable2.nickname from gifttable left join usertable as usertable1 on usertable1.id = gifttable.from left join usertable as usertable2 on usertable2.id = gifttable.to

SQL LEFT JOIN 关键字

人气教程排行