[SOLVED] Select from two tables statment

Issue

I have two tables:

Users:

id        name       isSpecial
1         Tal        1
2         Jorden     0
3         John       1
4         Paige      0

Details:

id        userId       Country       zipCode
1         1            Israel        4564
2         3            US            554654

I want to get all the data from Users by the name of Jorden OR if isSpecial is 1 to be shown like this

Result:

id        name         Country       zipCode
1         Tal          Israel        4564
2         Jorden       
3         John         US            554654

I know it’s supposed to be a simple query but I can’t get the results that I want!

Solution

You can use a LEFT JOIN:

SELECT
  u.id,
  u.name,
  u.isSpecial,
  d.country,
  d.zipCode
FROM Users u
LEFT JOIN Detals d
ON u.id = d.userId
WHERE u.name = 'Jorden'
  OR u.isSpecial = 1

Answered By – Ende Neu

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *