doufu8887
2015-04-10 08:43 阅读 33
已采纳

使用两个表中的条件从一个表中删除Mysql

I am using this code/query to delete bogus users using a list from 'bogus' table and Obviously this query is not correct and shows error: Unknown column 'bogus.user' in 'where clause' Consider that tables sample and bogus have ONLY ONE COLUMN each and I want to delete rows from sample table only retaining the data of table bogus.

delete from sample where sample.user=bogus.user;
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dongshi949737 dongshi949737 2015-04-10 08:48

    How about:

     delete from sample where sample.user in (SELECT user FROM bogus);
    

    I think that's the savest way. It's probably possible to put both tables in a single statment without a join or nested select. But If you do that wrong you risk deleting both tables content. Thus I'd say it's better to do it this way.

    点赞 评论 复制链接分享
  • dptrmt4366 dptrmt4366 2015-04-10 08:45

    You need to join for this

    delete s from sample s
    join bogus b on b.user = s.user
    
    点赞 评论 复制链接分享
  • doupu7651 doupu7651 2015-04-10 08:50
    delete from sample where user in (select user from bogus)
    
    点赞 评论 复制链接分享

相关推荐