[英]Mysql:multiple join in one table
I have a database and inside database are table with joins. 我有一个数据库,并且数据库内部是带有联接的表。
My first table is cardtests
the columns are id
and name
only. 我的第一个表是cardtests
列仅是id
和name
。 I joined it in tests
my columns are : 我参加了tests
我的专栏是:
card1_id | card1_id | card2_id | card2_id | card3_id card3_id
in which they are equal to cardtests.id. 它们等于cardtests.id。
My sqlquery is 我的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
But the problem is they are not working just wanted to display all the names of 3 cards. 但是问题在于,它们不能正常工作,只是想显示3张卡的所有名称。 How will i do it thanks. 我将如何做,谢谢。
Select cardtests.name
from tests
join cardtests on cardtests.id in (tests.card1_id, tests.card2_id, tests.card3_id)
I think you want three joins: 我认为您需要三个联接:
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;
The left join
simply handles rows where one or more of the card test columns may not be populated. left join
仅处理可能未填充一个或多个卡测试列的行。
Often, though, having multiple columns with the same name, only distinguished by a number, is an indication that you should be using a separate table, with one row per tests and card. 但是,通常会有多个具有相同名称的列,仅用数字来区分,这表明您应该使用单独的表,每个测试和卡片有一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.