如何删除名称但没有电话和电子邮件的用户

时间:2015-04-08 12:03:57

标签: mysql

我正在尝试删除mysql数据库,用户名字以及手机和电子邮件。 我得到了选择代码。但我不知道如何删除这些结果!

SELECT user.id FROM user, 
(SELECT emails.id
FROM
(SELECT user.id FROM email, user
    WHERE    
    email.user_id <> user.id) emails
INNER JOIN
(   SELECT user.id FROM phone, user
    WHERE    
    phone.user_id <> user.id) phones
ON emails.id = phones.id) as filter
WHERE
user.id = filter.id AND
user.name IS NOT NULL

如果我这样说:

DELETE FROM user
WHERE id IN
(SELECT user.id FROM user, 
(SELECT emails.id
FROM
(SELECT user.id FROM email, user
    WHERE    
    email.user_id <> user.id) emails
INNER JOIN
(   SELECT user.id FROM phone, user
    WHERE    
    phone.user_id <> user.id) phones
ON emails.id = phones.id) as filter
WHERE
user.id = filter.id AND
user.name IS NOT NULL)

我得到了 - &gt; #1093 - 您无法指定目标表&#39;用户&#39;用于FROM子句中的更新

非常感谢!

1 个答案:

答案 0 :(得分:1)

DELETE u
FROM `user` u
LEFT JOIN email e ON e.user_id = u.id
LEFT JOIN phone p ON p.user_id = u.id
WHERE u.name IS NOT NULL
AND p.user_id IS NULL 
AND e.user_id IS NULL