I have 3 tables connected with id's constructed like this:
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. Because rad_id=4 has strf_ID=4 and in STRANKEF table stf_ID=4 has data4 value.
Example quarry for rad_id=4 is:
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. 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. For more reference:
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
. When doing an INNER JOIN
, it only keeps rows where both tables match.
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.