gpt4 book ai didi

mysql - 如何使用left-join 编写mySQL 来解决这类问题?

转载 作者:行者123 更新时间:2023-11-29 01:33:09 24 4
gpt4 key购买 nike

如何针对此问题使用左连接构建 mySQL 查询?

Feed Table
-id
-lastpost_id (The id of the user from USER TABLE who last posted)
-firstpost_id (The id of the user from USER TABLE who first posted)

User Table
-id
-name

我想要一张这样的 table

Result 
-id
-lastpost_id
-lastpost_name
-firstpost_id
-firstpost_name

最佳答案

select *, a.name as lastpost_name, b.name as firstpost_name from Feed f 
left join user a on f.lastpost_id=a.id
left join user b on f.firstpost_id=b.id

诀窍是左连接,然后将表别名为 a 或 b。这样你就可以对同一个表进行多个连接

关于mysql - 如何使用left-join 编写mySQL 来解决这类问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5239475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com