I have 3 tables, VALUES, SETTINGS and CONTROLS. Every SETTING has one CONTROL both not necessarily has a VALUE.
VALUES SETTINGS CONTROLS
------ -------- --------
ID ID ID
SettingID SettingName SettingID
Value ProductFamilyID ControlName
OrderID
I have tried joins and unions but just can't get it working. What I would like is a query where the result would look like this:
SettingName ControlName Value
----------- ----------- -----
Setting1 Control1 Value1
Setting2 Control58 Value22
Setting3 Control22 null
I forgot to mention, I would like to filter the result where VALUE.OrderID = '1'
and also where SETTINGS.ProductFamilyID='2'
The problem I always face is, there are 52 SETTINGS but only 45 of them have VALUES. So with all the left joins, 7 SETTING is missing and I can only see the 45 which has VALUE.
With UNION I have something close but I have the results in separate rows (1 row for a CONTROL, another for its VALUE or no value)
SELECT
SETTINGS.ID, CONTROLS.ControlName
FROM
CONTROLS
INNER JOIN
Settings ON CONTROLS.SettingID = SETTINGS.ID
WHERE
SETTINGS.ProductFamilyID = '2'
UNION
SELECT
SETTINGS.ID, VALUES.Value
FROM
VALUES
INNER JOIN
SETTINGS ON VALUES.SettingID = SETTINGS.ID
WHERE
VALUES.OrderID = '1'
The result of this is:
SettingID ControlName
--------- -----------
1 Control1
1 Value1
2 Control2
2 Value2
3 Control3 <--- Control3 has no value
4 Control4
4 Value4
What I would like is to have the values in a separate column by the ControlName
column.