noOne noOne - 6 months ago 12
MySQL Question

two inner join in one sql statement

What's wrong here ? i just want to display all the item in item_tb with 2 different group , vicma and branch but it returns nothing. It only works in one inner join but when i join the other one it display nothing.

|-------------|-------------------------|---------------|
|item_tb | vicma_tb | branch_tb |
| | vID - PK | id-PK |
|branchID-FK | | |
|vicma - FK | | |
|-------------|-------------------------|---------------|


$sql = "
SELECT item_tb.*
, branch_tb.*
, vicma_tb.*
from item_tb
JOIN branch_tb
on item_tb.branchID = branch_tb.id
JOIN vicma_tb
on item_tb.vicma = vicma_tb.vID ";

Answer

Seems like you need to do a LEFT JOIN instead of INNER JOIN. LEFT JOIN will return all values from your original table and NULL if there is no match. Try:

SELECT  item_tb.*, branch_tb.* , vicma_tb.* from item_tb 
LEFT JOIN  branch_tb on item_tb.branchID = branch_tb.id 
LEFT JOIN vicma_tb on item_tb.vicma = vicma_tb.vID