简体   繁体   English

MySQL Select语句where table1.id!= table2.id

[英]MySQL Select statement Where table1.id != table2.id

I have a table of data which has posts, then I have a separate table of data which has deleted posts. 我有一个包含帖子的数据表,然后我有一个单独的数据表,删除了帖子。 What happens when a post is deleted is that it's ID get's added to the deleted table rather than removing the post entry. 删除帖子后会发生的情况是,它的ID被添加到已删除的表中而不是删除帖子条目。

What is a clean efficient way of selecting all the posts from the posts table without selecting the ones that have their ID in the deleted table 什么是从posts表中选择所有帖子而不选择在已删除表中具有其ID的那些帖子的干净有效方式

Would it not be easier to add an extra column to your posts table and store and boolean value of deleted? 在posts表中添加一个额外的列并删除存储和布尔值会不会更容易? Or use an integer field (to represent user id) if you want to store who deleted it, 0 = not deleted. 或者如果要存储删除它的人,则使用整数字段(表示用户ID),0 =未删除。

If you can't change your tables, you can do a Left Join (which will join your deleted_table when possible) and then check for the id to be Null (means that no row has been found). 如果你不能改变你的表,你可以做一个Left Join (在可能的情况下将加入你的deleted_table ),然后检查idNull (意味着没有找到任何行)。

Select everything_you_need
From post_table p
Left Join delete_table d On ( d.id = p.id )
Where d.id Is Null;

Make sure to have indexes on both id -columns for best performance. 确保在两个id -columns上都有索引以获得最佳性能。

If you have a good reason not to use a boolean flag as bigstylee suggests, you can use an EXISTS subquery: 如果你有充分的理由不像bigstylee建议的那样使用布尔标志,你可以使用EXISTS子查询:

SELECT * FROM table1 
  WHERE NOT EXISTS 
  (SELECT * FROM table2 WHERE table1.id=table2.id);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP MYSQL JOIN QUERY 2 数据库,其中 table1.id = table2.id,如何将 table2.content 显示为 table1.id - PHP MYSQL JOIN QUERY 2 Databases, Where table1.id = table2.id, How to display table2.content as table1.id SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE子句 - SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE clause MySQL使用table1.id REGEXP table2.id为同一组选择不同的行 - MySQL select distinct rows for same group using table1.id REGEXP table2.id Mysql - UPDATE表SET列= SELECT COUNT(*)FROM(SELECT * FROM table2 WHERE table2.id = table.id))不可能 - Mysql — UPDATE table SET column = SELECT COUNT(*) FROM ( SELECT * FROM table2 WHERE table2.id = table.id ) ) Impossible 当我使用此php时,结果显示所有表而不是table1.id = table2.id的特定条件 - When i use this php the results show all the table not the specific condition that table1.id = table2.id 当table1.id = table2.id时如何打印一件事,而如果“ if else”又如何打印? - How can I print one thing when table1.id=table2.id, and another thing 'if else'? Laravel 一页杂物 Select 全部来自 table2 其中 id = table1.id - Laravel one page crud Select all from table2 where id = table1.id 连接两个表,其中table1.id等于table2.table1_id,但仅显示table1.id中在table2.table1_id中找不到的行 - Join two tables where table1.id equals table2.table1_id, but only display rows from table1.id that cannot be found in table2.table1_id 选择id在其他表中的语句 - Select statement where id is in other table PHP MYSQL - 突出显示数据库表行IF table1.id存在于table2中 - PHP MYSQL - Highlight a database table row IF table1.id exists in table2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM