简体   繁体   English

MySQL查找一个表中的记录,而该表没有另一表中的列的值

[英]MySQL Find records in one table that has no value of a column in another table

I have table1, containing columns (simplified): 我有table1,其中包含列(简体):

+-------------------------+
| id | user_id | username |
+----+---------+----------+
|  1 |     123 | peter    |
|  2 |     234 | john     |
+-------------------------+

and table 2, containing columns (simplified): 和表2,其中包含列(简体):

+----------------------------------+
| id | user_id | checklist_item_id |
+----+---------+-------------------+
|  1 |     123 |               110 |
|  2 |     123 |               111 |
|  3 |     123 |               112 |
|  4 |     234 |               110 |
|  5 |     234 |               112 |
+----------------------------------+

As shown above, Each entry for user_id from table1, has multiple entries for that user_id with multiple checklist_item_ids. 如上所示,表1中user_id的每个条目都有该user_id的多个条目以及多个checklist_item_ids。

I am interested in returning ONLY records that does NOT have an entry in the second table for checklist_item_id = 111. The query must return only: 我有兴趣返回仅第二张表中没有checklist_item_id = 111条目的记录。查询必须仅返回:

+---------+
| user_id |
+---------+
|     234 |
+---------+

As user with user_id 123 DO have an entry in table two with checklist_item_id of 111. 作为具有user_id 123的用户,请在表2中具有一个checklist_item_id为111的条目。

You can use subquery, for example: 您可以使用子查询,例如:

SELECT *
FROM table1
WHERE user_id NOT IN
    (SELECT user_id
     FROM table2
     WHERE checklist_item_id = 111)

use corelated subquery 使用相关子查询

select t1.* from  table1 t1 where t1.user_id not in
( select user_id from table2 t2
   where t2.user_id=t1.user_id
   and checklist_item_id=111
)

Or use not exist which is efficient than not in 或使用not exist比不使用更有效的方法

select t1.* from table1 t1 where not exists
    ( select 1 from table2 t2  where t2.user_id=t1.user_id and 
    checklist_item_id=111
    )    

DEMO in Fiddle 小提琴中的演示

id  userid  itemid
4   234     110
5   234     112

In case you need only one id then it would be 如果您只需要一个ID,那么它将是

select distinct t1.userid from  t1 where not exists
    ( select 1 from t1 t2  where t2.userid=t1.userid and 
    itemid=111
    )    

output 输出

userid
  234

Demo 演示版

Simplest and efficient approach would be using LEFT JOIN , and filtering out those rows, where there is no matched record for checklist_item_id = 111 最简单有效的方法是使用LEFT JOIN ,并过滤掉那些没有checklist_item_id = 111匹配记录的行。

SELECT DISTINCT t1.user_id 
FROM table1 AS t1 
LEFT JOIN table2 AS t2 ON t2.user_id = t1.user_id AND 
                          t2.checklist_item_id = 111 
WHERE t2.user_id IS NULL 

暂无
暂无

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

相关问题 MySQL查询以查找一个表中一列的值是否在另一表中两列的两个值之间 - MySQL query to find if a value of one column in one table is between two values in two columns on another table 需要逻辑来找到 mysql 中同一表中另一列的最小值的一列的值 - Need the logic to find the value of one column for the min of another column in same table in mysql 根据mysql中另一个表中的值更新一个表中的列 - Update column in one table based on value in another table in mysql 如何在MySQL的另一个表中显示具有多个值的列的值 - How to show value that has column with multiple value in another table in MySQL 当两个表字段在MySQL中具有相同的值时,如何忽略将表中的记录插入到另一个表中? - How to ignore inserting records from a table to another table when both table field has same value in MySQL? MySQL-对一个表中的记录进行计数,并对另一个表中的记录求和 - MySQL - Count records in one table and sum records in another table mySql查询查找另一个表中不存在的记录 - mySql query to find records not present in another table 将一个表的不同记录插入另一个到MySQL - Insert distinct records of one table to another into MySQL MySQL timediff在表中选定记录的一列上 - MySQL timediff on one column for selected records in table MySQL-根据另一张表的列数从一张表中获取记录 - MySQL - Get records from one table based on count of another table's column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM