Reputation: 26598
I have 3 tables in a MySql Database:
I have to retrieve Companies that has got a specific date in date field of Company, and that meet the following rule: There should be no records in Object table that has idP=xx and idI=(an id of a company that has got the specific value date yy-mm-dd)
How can I write this query?
Upvotes: 0
Views: 55
Reputation: 10635
SELECT * FROM Company
LEFT JOIN Object ON( Object.idP = xx OR Object.idl = Company.id )
WHERE Company.date = somedate AND Object.idP IS NULL
Upvotes: 1
Reputation: 4045
SELECT * FROM Company WHERE data = '2011-08-09' AND
Company.id NOT IN
(SELECT Object.idI FROM Object WHERE Object.idI = Company.id AND Object.idP = 'XX')
But I have to say that your columns and tables naming need to be re-done. It could be a lot more meaningful.
You have to created indexes for Object.idI and Object.idP to make it run quicker.
Upvotes: 1