gpt4 book ai didi

mysql - 如何通过mysql中的多个表在单个查询中获取所有数据(就像名字,父亲,母亲,自己的名字)数据?

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

我有两张 table 。就像

----用户表----

id  user    email       
1 admin admin@gmail.com
2 editor editor@gmail.com

----姓名表----

name    userid  fullname    mobile
own 1 Rahim 012314
father 1 Karim 120120
mother 1 Florin 212021

如何在 mysql 中的单个查询中获取所有数据(就像姓名、父亲、母亲、自己的名字)数据?

最佳答案

为每个名字加入一次名字表:

select id, u.user, email, o.fullname own, f.fullname father, m.fullname mother
from users u
left join nametable o on o.userid = u.id and o.name = 'own'
left join nametable f on f.userid = u.id and f.name = 'father'
left join nametable m on m.userid = u.id and m.name = 'mother'

请注意名称表上的所有条件都必须在连接条件中才能使连接保持为外部连接。

关于mysql - 如何通过mysql中的多个表在单个查询中获取所有数据(就像名字,父亲,母亲,自己的名字)数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35458550/

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