gpt4 book ai didi

mysql - mysql中的条件连接

转载 作者:IT老高 更新时间:2023-10-28 23:57:29 24 4
gpt4 key购买 nike

我有一个表 id1id2typetype 是一个枚举值,包含另一个表的名称。我想使用 type 表的名称来执行 join。例如:

switch($type)
case 'table1':
join table1;
break;
case 'table2':
join table2;
break;

我怎样才能做到这一点?

最佳答案

你不能直接那样做......你可以这样做(不是很漂亮......):

SELECT
t.id,
t.type,
t2.id AS id2,
t3.id AS id3

FROM t
LEFT JOIN t2 ON t2.id = t.id AND t.type = 't2'
LEFT JOIN t3 ON t3.id = t.id AND t.type = 't3'

关于mysql - mysql中的条件连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1255492/

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