Search code examples
mysqlsqldatabaseinner-join

MYSQL Two inner joins not producing a result


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

Solution

  • 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;