mysql - 我想选择不遵循 where 条件或 "except where condition"的行

标签 mysql sql database

我有两张 table 。 首先是“tab_task”

   task_id|   task_name  | created_by | status
   -------------------------------------------
     1    |    task1     |    aarav   | 1
     2    |    task2     |    rahul   | 0
     3    |    task3     |    aarav   | 0
     4    |    task4     |    jasmin  | 0
     5    |    task5     |    yamini  | 1
     6    |    task6     |   priyanka | 0
     7    |    task7     |    manish  | 1
     8    |    task8     |    aarav   | 1

第二个表是“tab_user”

   user_id| txt_full_name|
   -------------------------
     1    |    aarav     | 
     2    |    rahul     |  
     3    |   yamini     |   
     4    |   jasmin     |    
     5    |   manish     |   
     6    |  priyanka    |  

SELECT created_by from tab_task where status='1' 现在我们有 4 行“aarav、yamini、manish 和 aarav”。

现在我想从“tab_user”获取“txt_full_name”,其中“txt_full_name”不等于“tab_task's created_by”。 我的意思是我想从“tab_user”获取:rahul、jasmin、priyanka。

最佳答案

一个简单的解决方案:

SELECT txt_full_name FROM tab_user
    WHERE txt_full_name NOT IN (SELECT created_by from tab_task where status='1')

(请注意,上面假设 created_by 列是 NOT NULL,否则查询不会返回任何行,因为与 NULL 比较产生 UNKNOWN - fiddle - doesn't work with NULL data )。

关于mysql - 我想选择不遵循 where 条件或 "except where condition"的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41846272/

相关文章:

sql - 如何从表中选择每一行,并按连接表中最相关的 "recent (timestamp)"行对它们进行排序?

Android:SQLiteCursor/SimpleCursorAdapter 在后台

mysql - MariaDB 使用 utf8mb4 时出现错误

mysql - SQL INNER JOIN 和 AVG() 返回错误数据

mysql 加入一个列

php - Mysql 结果和数组操作

php - 如何在单个查询中执行多个选择查询并仍然检索值

sql - 如何比较两个表并返回与 HIVE 不同的行

javascript - 为什么我的 addeventlistener 只能在一个按钮中工作?

sql-server - 无法使用 SSMS 通过 Windows 身份验证连接到本地 SQL Server