RE: Negated SELECT query
"Bonnett, John" <[email protected]> Wed, 18 Mar 2009 09:29:05 +1030
| Newsgroups | gmane.comp.db.mysql.general,gmane.comp.db.mysql.windows |
|---|---|
| Message-ID | <[email protected]> |
SELECT Employees.* FROM Employees LEFT JOIN Assets ON Employess.EmployeeID =3D Assets.EmployeeID =20 WHERE Assets.EmployeeID IS NULL The one for assets with no maintenance is similar. The point is the left join above produces in its output all rows from the Employees table regardless of whether anything matches in the assets table. By selecting only rows where the foreign key field in the assets table is null gives you the employees having no assets. John Bonnett -----Original Message----- From: BobSharp [mailto:[email protected]]=20 Sent: Wednesday, 18 March 2009 3:13 AM To: [email protected] Cc: [email protected]; [email protected] Subject: Negated SELECT query 3 tables are related by one-many links.=20 Employees ---<- Assets ---<- Maintenance=20 Employees can be assigned =3D> 0 Assets=20 Assets can have =3D> 0 occurances of Maintenance. =20 Assets table contains EmployeeIDs and MaintenanceIDs, but no Foreign Key contraints. =20 Queries ... =20 1) which Employees do not have any Assets ?=20 2) which Assets have not had any Maintenance ?=20 These have been written successfully with Sub-Queries, =20 I would like to know how they can be done with only JOINs ?=20 (that also means without the EXCEPT statement)=20 Is that too much of a challenge ? (MySQL 5.0.67) =20 -- I am using the free version of SPAMfighter. We are a community of 6 million users fighting spam. SPAMfighter has removed 12747 of my spam emails to date. Get the free SPAMfighter here: http://www.spamfighter.com/len The Professional version does not have this message -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/[email protected]