SQL query to find records without references
I have two tables linked via an ID field:
table1: id, name, type
table2: id, id_table1, date, status
I need to collect all records table1
that have a specific value in a field type
and that are not listed in table2
, as well as all records table1
that are referenced in table2
that have a specific field value status
.
For the first part, if I remember correctly, I can use the command LEFT JOIN
:
LEFT JOIN table1.name
LEFT JOIN table2
ON table2.id_table1 = table1.id
WHERE (table1.value = 'value1') AND (table2.id_table1 IS NULL);
but for the second part I am lost ...
I am using MySQL 5.6 and I would like to define a view to handle this.
source to share