gpt4 book ai didi

mysql - 如何获取另一个表的字段信息

转载 作者:行者123 更新时间:2023-11-30 00:15:00 25 4
gpt4 key购买 nike

我有三个表,user/team/user_team(多对多)

user
-----------
id name
1 Tom
2 Jerry
3 John

team
------------
id name
1 t1
2 t2
3 t3

user_team
---------------------
userid teamid isdeleted
1 t1 0 <----(0 means not deleted record, which can be searched out)
2 t2 1 <----(1 means deleted record, which can not be searched out)

我想获取所有团队记录信息以及关联的用户信息,如下所示

--------------
tid tname username
1 t1 Tom
2 t2
3 t3

能告诉我sql语句怎么写吗?

很抱歉我的错误。我通过在团队表中添加一条记录 t3 更新了我的问题。

最佳答案

试试这个:

Select a.id as tid
a.name as tname
b.name as username
from team a
LEFT JOIN user_team c on a.name = c.teamid and c.isdeleted = 0
LEFT JOIN user b on b.id = c.userid

关于mysql - 如何获取另一个表的字段信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23728687/

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