Forum Moderators: phranque
SELECT table1.myfield1, table2.myfield2
FROM table1, table2
WHERE table1.key=table2.key AND myfield1="something"
AND myfield2 NOT IN
(SELECT myfield2 FROM table3 WHERE table3.myfield3="whatever")
For example, if I wanted to get all Forum Posts (Table1) for TopicA that do NOT have replies (Table3) by User1.
Any help would be greatly appreciated!
Starting with version 4.1, MySQL supports all subquery forms and operations which the SQL standard requires, as well as a few features which are MySQL-specific.
For MySQL versions prior to 4.1, most subqueries can be successfully rewritten using joins and and other methods. Rewriting Subqueries for Earlier MySQL Versions [mysql.com].
You are probably going to end up using a
LEFT JOIN where the linked value in the right hand table IS NULL. SELECT
table1.myfield1, table2.myfield2
FROM table1
INNER JOIN table2
ON table1.key = table2.key
LEFT JOIN table3
ON table2.myfield2 = table3.myfield3
WHERE table1.myfield1="something"
AND table3.myfield3="whatever"
AND table2.myfield2 IS NULL
;
I don't have a database structured as such to test the statement, but it should give you a start.