gpt4 book ai didi

mysql - 表的动态连接 (MySQL)

转载 作者:行者123 更新时间:2023-11-29 09:10:05 25 4
gpt4 key购买 nike

我正在尝试找出在给定设置中连接表的正确方法:

表0:第 1 列第2栏第3栏科尔克斯科利

Col1-3 是 3 个不同表的外键 - 它们可能有也可能没有值(例如,col1 可能为空,或者 col 2 和 3 都为空,或者都没有)。我想做的是构建一个选择查询,当且仅当一个值存在时才连接到表 1 - 3。

我希望我能很好地解释这一点。

最佳答案

听起来您只是要求左连接(或另一种 outer join ):

select table0.*, user.*, show.*
from table0
left join user on user.id=table0.userid
left join show on show.id=table0.showid

典型的结果可能是

table0.id  table0.userid  table0.showid  user.id  user.name  show.id  show.name
1 1 NULL 1 Bob NULL NULL
2 NULL 1 NULL NULL 1 Flintstones

关于mysql - 表的动态连接 (MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5838772/

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