I am trying to join two tables while still getting the results from table A even if there is no matching record in table B. I think the where clause is causing the problem but I can't seem to fix it.
SELECT a.id, a.title, a.validation, b.data
FROM client_option_detail AS a LEFT OUTER JOIN client_data AS b
ON a.id = b.client_option_detail_id
WHERE a.client_option_id = ?
AND (b.client_id IS NULL OR b.client_id = ?)
UPDATE: database layout
client_option client_option_detail
------------- -------------------
id id
title
validation
client_option_id
client client_data
------- ------------
id client_id
client_option_detail_id
data
database sample:
client_option client_option_detail
------------- -------------------------------------------
id id | title | validation | client_option_id
------------- -------------------------------------------
1 1 | test1 | | 1
2 2 | test2 | | 1
3 | test3 | | 1
4 | test4 | | 2
client client_data (primary key - client_id + client_option_detail_id)
-------- -------------------------------------------
id client_id | client_option_detail_id | data
-------- -------------------------------------------
1 1 | 1 | data1
2 1 | 2 | data2
1 | 4 | data3
2 | 3 | data4
2 | 1 | data5
sample query:
SELECT a.id, a.title, a.validation, b.data
FROM client_option_detail AS a LEFT OUTER JOIN client_data AS b
ON a.id = b.client_option_detail_id
WHERE a.client_option_id = 1
AND (b.client_id IS NULL OR b.client_id = 1)
required output:
-------------------------------------------
id | title | validation | data
-------------------------------------------
1 | test1 | | data1
2 | test2 | | data2
3 | test3 | |