简体   繁体   English

MYSQL 两个内连接不产生结果

[英]MYSQL Two inner joins not producing a result

I have 3 tables connected with id's constructed like this:我有 3 个与 id 连接的表,结构如下:

RAD table
rad_id      strp_ID     strf_ID
1               1           null
2               1           null
3           null            3
4           null            4
5           null            4

STRANKEP table
strp_ID     strp_NAZIV
1           data1
2           data2
3           data3

STRANKEF table
strf_ID     strf_NAZIV
1           data1
2           data2
3           data3
4           data4

I'm trying to get for example value of strf_NAZIV witch is data4 in case rad_id=4.我正在尝试获取例如 strf_NAZIV 女巫的值是 data4,以防 rad_id=4。 Because rad_id=4 has strf_ID=4 and in STRANKEF table stf_ID=4 has data4 value.因为 rad_id=4 有 strf_ID=4 并且在 STRANKEF 表中 stf_ID=4 有 data4 值。

Example quarry for rad_id=4 is: rad_id=4 的示例采石场是:

SELECT rad_id, strp_NAZIV, strf_NAZIV FROM RAD 
INNER JOIN STRANKEP ON RAD.strp_ID=STRANKEP.strp_ID 
INNER JOIN STRANKEF ON RAD.strf_ID=STRANKEF.strf_ID 
WHERE rad_id = 4;

When I run the quarry I get 0 rows result with no errors and correct columns.当我运行采石场时,我得到 0 行结果,没有错误和正确的列。 I can not get my head around this, please advise.我无法解决这个问题,请指教。

rad_id strp_NAZIV strf_NAZIV
0 rows

Okay, the first thing is that inner join only returns results where an attribute of a tuple exists in both tables, when one of the table contains null values for eg RAD, the tuples with ID 3, 4, 5 won't be returned, from the result of first inner join and 1 and 2 from 2nd.好的,第一件事是内连接只返回两个表中都存在元组属性的结果,当一个表包含空值时,例如 RAD,ID 为 3、4、5 的元组将不会被返回,从第一个内部连接的结果和第二个的 1 和 2 的结果。 For more reference:更多参考:

https://www.w3schools.com/SQL/sql_join_inner.asp https://www.w3schools.com/SQL/sql_join_inner.asp

Try using outer joins instead of inner to get the results with null values.尝试使用外连接而不是内连接来获得空值的结果。

There is no STRANKEP table where strp_ID = 4 .没有strp_ID = 4 STRANKEP表。 When doing an INNER JOIN , it only keeps rows where both tables match.执行INNER JOIN ,它只保留两个表匹配的行。

Maybe you want this:也许你想要这个:

SELECT rad_id, strp_NAZIV, strf_NAZIV 
FROM RAD 
LEFT JOIN STRANKEP ON RAD.strp_ID=STRANKEP.strp_ID 
LEFT JOIN STRANKEF ON RAD.strf_ID=STRANKEF.strf_ID 
WHERE rad_id = 4;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM