Anyone want to try to help me with this. I have a DB with 2 tables (users and instance) a bunch of records were deleted from the users table, and I want to find record id numbers and a record name from the instance table that have userid's that have been deleted and that are of a type "Catagory".... I've got most of the statement working, but the "userid not in users.userid" doesn't because I'm not sure how to refer to the column as a list....
SELECT t1.userid, t1.iid, t1.name FROM instance t1, users t2 WHERE ( ??? ) AND ( t1.IsA = "Category" ) ORDER BY t1.userid ASC;
SELECT t1.userid, t1.iid, t1.name FROM instance t1, users t2 WHERE ( ??? ) AND ( t1.IsA = "Category" ) ORDER BY t1.userid ASC;
no subject
Date: 2007-08-09 04:02 am (UTC)I think you want something like:
SELECT t1.userid, t1.iid, t1.name FROM instance t1
WHERE t1.userid NOT IN (select userid from users)
AND ( t1.IsA = "Category" )
ORDER BY t1.userid ASC;
no subject
Date: 2007-08-09 04:45 am (UTC)SELECT i.userid, i.iid, i.name
FROM instance i
LEFT JOIN users u
ON i.userid=u.id
WHERE u.id IS NULL and i.IsA='Category'
BTW, you will burn in hell for using the aliases "t1" and "t2".
no subject
Date: 2007-08-09 04:50 am (UTC)no subject
Date: 2007-08-09 05:15 am (UTC)Yeah I'll be burning in hell for SOO many other reasons, that coding style points will have to wait their turn. :-)
no subject
Date: 2007-08-09 04:46 am (UTC)The correct statement is an outer join and not actually more work than the "not in".
SELECT t1.userid, t1.id, t1.name
FROM instance t1 LEFT OUTER JOIN users t2
USING (userid)
WHERE t2.userid IS NULL AND t1.IsA = "Category"
ORDER BY t1.userid ASC;
the LEFT OUTER JOIN takes everything in t1 and attempts to match it with a corresponding userid in t2 (the USING clause) -- if it doesn't match, t2.userid will be NULL.
Love your friendly neighborhood She-BA
no subject
Date: 2007-08-09 04:57 am (UTC)No doubt yours is the better statement, although the 'using' might need to be an 'on' depending on the database.
no subject
Date: 2007-08-09 05:10 am (UTC)Some days you just stare at the problem too long...
no subject
Date: 2007-08-09 12:41 pm (UTC)