gpt4 book ai didi

php - Mysql:一张表中的多个连接

转载 作者:行者123 更新时间:2023-11-29 07:35:54 25 4
gpt4 key购买 nike

我有一个数据库,内部数据库是带有联接的表。

我的第一个表是cardtests,列只有idname。我在测试中加入了它,我的专栏是:

card1_id | card2_id | card3_id

in which they are equal to cardtests.id.

我的sqlquery是

SELECT cardtests.name 
FROM tests
JOIN cardtests
ON tests.card1_id = cardtests.id
ON tests.card2_id = cardtests.id
ON tests.card3_id = cardtests.id

但问题是它们不起作用,只是想显示 3 张卡的所有名称。我该怎么做谢谢。

最佳答案

我认为你想要三个连接:

Select ct1.name, ct2.name, ct3.name
from tests t left join
cardtests ct1
on t.card1_id = ct1.id left join
cardtests ct2
on t.card2_id = ct2.id left join
cardtests ct3
on t.card3_id = ct3.id;

左连接仅处理可能未填充一个或多个卡测试列的行。

但是,通常情况下,多个列具有相同的名称,仅通过数字进行区分,这表明您应该使用单独的表,每个测试和卡一行。

关于php - Mysql:一张表中的多个连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30836728/

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