I have three tables with the following data:
varieties
------------------------------------
id | variety | status |
------------------------------------
1 | VS1 | 1 |
------------------------------------
2 | VS2 | 1 |
------------------------------------
3 | VS3 | 1 |
------------------------------------
4 | VS4 | 1 |
------------------------------------
5 | VS5 | 1 |
------------------------------------
units
----------------------------------------------
id | id_grouper | unit | status |
----------------------------------------------
1 | 13 | 1 | 1 |
----------------------------------------------
2 | 13 | 2 | 1 |
----------------------------------------------
3 | 13 | 3 | 1 |
----------------------------------------------
4 | 13 | 4 | 1 |
----------------------------------------------
5 | 13 | 5 | 1 |
----------------------------------------------
6 | 13 | 6 | 1 |
----------------------------------------------
7 | 13 | 7 | 1 |
----------------------------------------------
sowing
-----------------------------------------------------------------
id | id_grouper | id_unit | id_variety | type | status |
-----------------------------------------------------------------
1 | 13 | 1 | 1 | SW | 1 |
-----------------------------------------------------------------
2 | 13 | 3 | 1 | SW | 1 |
-----------------------------------------------------------------
3 | 13 | 4 | 1 | SW | 1 |
-----------------------------------------------------------------
4 | 13 | 6 | 3 | SW | 1 |
-----------------------------------------------------------------
5 | 13 | 7 | 4 | ER | 1 |
-----------------------------------------------------------------
The table varieties shows me varieties, the table units are beds and the table sowing are symmetrical, in the latter it records that varieties planted There is in each bed and the type of planting.
The type SW means that it is currently planted and ER that was eradicated
What I want to do is show the varieties sown in each bed with type SW and if a bed is not in the table sowing or it is type ER that the bed with variety NULL likewise shows me in the following way:
----------------------------------
unit | variety |
----------------------------------
1 | VS1 |
----------------------------------
2 | NULL |
----------------------------------
3 | VS1 |
----------------------------------
4 | VS1 |
----------------------------------
5 | NULL |
----------------------------------
6 | VS3 |
----------------------------------
7 | VS4 |
----------------------------------
I made the following query using LEFT JOIN
but it does not work for me:
SELECT units.unit, varieties.variety
FROM (sowing
LEFT JOIN units ON sowing.id_unit = units.id)
LEFT JOIN varieties ON sowing.id_variety = varieties.id
WHERE units.id_grouper = 13
AND sowing.type= 'SW'
AND sowing.status = 1
AND units.status = 1
GROUP BY units.id;
returns the following result:
----------------------------------
unit | variety |
----------------------------------
1 | VS1 |
----------------------------------
3 | VS1 |
----------------------------------
4 | VS1 |
----------------------------------
6 | VS3 |
----------------------------------
7 | VS4 |
----------------------------------
I'm a bit of a rookie with this kind of questions, any comments or help is welcome, I hope you understand the question.