I have this table schema.
Machine(machine_id, size)
Operator(operator_id, name)
OperationLog(machine_id, operator_id, date, comment)
machine_id: FK(Machine)
operator_id: FK(Operator)
Assuming I want a query that only gives me the name of the operators that operated all machines with size above 5m2
Would using the ALL operator give me the desired result? As in, from the list of Machines with size above 5, Operators that have Logs must match all of those machines.
SELECT O.name
FROM Operator O NATURAL JOIN OperationLog L
WHERE L.machine_id = ALL (
SELECT M.machine_id
FROM Machine M
WHERE size >5);
Or would I need to do a "double negation" like so?
SELECT O.name
FROM Operator O
WHERE NOT EXISTS(
SELECT M.machine_id
FROM Machine M
EXCEPT
SELECT L.machine_id
FROM OperationLog L NATURAL JOIN Machine M
WHERE L.operator_id = O.operator_id
AND size >5);
I feel I am complicating too much and there's probably a better way.
relational division. That term plusmysqlshould find you plenty of examples and approaches.ALLsubquery results in true when it returns only a single machine_id (one or multiple rows).