I have a problem that I haven't been able to resolve for some hours.
I have 3 tables:
Process
\n+----+----------+----------------+--------+-------------+---------------+-------------+---------+------+ \n| id | name | description | active | responsible | informByEmail | informBySms | remarks | icon | \n+----+----------+----------------+--------+-------------+---------------+-------------+---------+------+ \n| 4 | Process1 | TestDecriptino | 1 | 0 | 0 | 0 | 0 | NULL | \n| 5 | Process2 | test | 0 | 0 | 0 | 0 | test | NULL | \n| 6 | Process3 | 12322 | 1 | 0 | 0 | 0 | 12322 | NULL | \n| 7 | Process4 | 222222222222 | 0 | 0 | 0 | 0 | 2222222 | NULL | \n| 9 | Process5 | sgdasad | 1 | 0 | 1 | 0 | dhds | NULL | \n+----+----------+----------------+--------+-------------+---------------+-------------+---------+------+ \n
Systems
\n+----+---------+-------------+--------+-------------------+---------------+-------------+---------+------+ \n| id | name | description | active | responsibleUserId | informByEmail | informBySms | remarks | icon | \n+----+---------+-------------+--------+-------------------+---------------+-------------+---------+------+ \n| 2 | Sistem1 | fdjgf | 1 | 1 | 1 | 1 | 0 | NULL | \n| 6 | Sistem2 | koam | 0 | 3 | 1 | 0 | SADGS | NULL | \n+----+---------+-------------+--------+-------------------+---------------+-------------+---------+------+ \n
Process_Systems
\n+----+-----------+----------+--------+---------+ \n| id | processId | systemId | active | remarks | \n+----+-----------+----------+--------+---------+ \n| 4 | 4 | 2 | 1 | aa | \n| 8 | 7 | 2 | 1 | aa | \n| 11 | 9 | 2 | 1 | aa | \n| 15 | 4 | 6 | 0 | aa | \n+----+-----------+----------+--------+---------+ \n
I have a method to which processID
is a parameter and must somehow filter all Process_Systems
by that ID and then get Systems
which do not belong to the Process_Systems
table.
One way would be to use a NOT IN
subquery :
SELECT * FROM Systems WHERE id NOT IN ( SELECT systemid FROM Process_Systems WHERE processId = ? );
Another would be to use a NOT EXISTS
subquery :
SELECT * FROM Systems WHERE NOT EXISTS ( SELECT * FROM Process_Systems WHERE systemId = Systems.id AND processId = ? );
A third way would be to use an outer join :
SELECT Systems.* FROM Systems LEFT JOIN Process_Systems ps ON ps.systemId = Systems.id AND ps.processId = ? WHERE ps.systemId IS NULL;
See them on sqlfiddle .
For an analysis of the respective performance differences, see @Quassnoi's blog articles:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.