Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JOIN three tables even if one is empty

Tags:

sql

mysql

I'm trying to get values from three tables even if one is empty. Since I would be able to use an if else statement depending if the following.follow_id row is empty or not.

SELECT user.id, user.username,user.email, userdetails.profile_img, following.follow_id 
FROM user 
JOIN userdetails ON user.id = userdetails.user_id 
JOIN following ON user.id = following.follow_id 
GROUP BY user.id;

For now it only gives me the value if there is a value on row following.follow_id. But I would be able to get all the result from the user_id even if there's an empty table.

Any clues ?

like image 568
Dymond Avatar asked Aug 31 '25 06:08

Dymond


1 Answers

Replacing "JOIN", which implies "INNER JOIN", by "LEFT JOIN" should do the trick.

like image 119
Patrick Kostjens Avatar answered Sep 03 '25 07:09

Patrick Kostjens