I come across a query which is,
SELECT it1.survey_set_id, it1.type, it1.value FROM survey_condition_filter it1 LEFT JOIN survey_condition_filter it2 ON(it1.survey_set_id = it2.survey_set_id AND it2.type = 3002) WHERE it1.type IN (2000, 2001, 2002) AND it2.value IS NULL;
Why is self left-join is used in the above query.
SELECT survey_set_id, type, value FROM survey_condition_filter WHERE type IN (2000, 2001, 2002);
isn't the above query is equivalent to the first query which used self left-join. since the query is just also filtering the IN (2000, 2001, 2002) AND it2.value IS NULL
. I am confused the use of join query here and can't really understand the working of the first query.
This is table survey_condition_filter
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| survey_id | bigint | NO | PRI | NULL | |
| survey_set_id | bigint | NO | PRI | NULL | |
| type | int | NO | PRI | NULL | |
| condition | tinyint | NO | PRI | NULL | |
| value | varchar(15) | NO | PRI | NULL | |
| display_value | text | NO | | NULL | |
| order | int | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| created_by | varchar(255) | YES | | NULL | |
| updated_at | datetime | NO | | NULL | |
| updated_by | varchar(255) | YES | | NULL | |
| deleted_at | datetime | YES | | NULL | |
| deleted_by | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+-------+