dongruyan4948 2014-06-15 00:54
浏览 5
已采纳

too long

How would I run a code where it checks table1 for the column postid and then searches table2 to see if that value actually exists. If it does exist then move onto the next but if it doesn't it removes the row from table1.

So if in table1 I have a row which has 3 rows: a postid of 333 another of 444 and another of 555.

Let's say 333 doesn't exist in table2 then I need to delete the row which contains that postid from table1 and move onto the next. It keeps doing that until all the rows are checked.

How would I accomplish this? I am very confused and haven’t been able to think of everything.

  • 写回答

1条回答 默认 最新

  • dongqiang4819 2014-06-15 01:12
    关注

    If you're iterating the post ids, you can perform this type of delete for each postid:

    DELETE FROM table1 WHERE postid = 333 AND 333 NOT IN (SELECT postid FROM table2 WHERE postid = 333);
    

    Or, delete all rows from table1 that does not have a record with a matching postid in table2 (assuming table2.postid column is defined as NOT NULL):

    DELETE table1 FROM table1 LEFT JOIN table2 ON (table2.postid = table1.postid) WHERE table2.postid IS NULL;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度