Re: Delete Subquery
| From: | Rui Cunha | Date: | Tue, 15 Jun 2004 08:34:46 +0000 |
| Subject: | Re: Delete Subquery | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-34788@lists.php.net to get a copy of this message | ||
"...I want to be able to delete a rowid from the ROLES TABLE, but only if the roleid does not exist in the USERS TABLE..."
DELETE FROM roles
WHERE roleid NOT IN ( SELECT DISTINCT roleid FROM users);
Rui
Chris Bolt writes:
How about: DELETE FROM users WHERE userid IN (SELECT u.userid FROM users AS u LEFT JOIN roles AS r ON (u.roleid = r.roleid) WHERE r.roleid IS NULL LIMIT 1); On Mon, 14 Jun 2004 11:59:25 -0400, Brock Jimmy D Contr 74 MDSS/SGSI <jimmy.brock@wpafb.af.mil> wrote:I've been able to figure how to use a delete subquery to delete all rows, but how do I use a delete subquery to delete only one row? I have two tables: users and roles with the following fields: Users: userid roleid Roles: roleid I want to be able to delete a rowid from the roles table, but only if the roleid does not exist in the users table. thanks