从where子句

时间:2019-06-28 09:15:37

标签: mysql

我有一组用户ID: (512,5,13,14,67)  和包含以下内容的表:

+----+--------+
| Id | userID |
+----+--------+
|  1 |    512 |
|  2 |     13 |
|  3 |     14 |
|  4 |     51 |
|  5 |      6 |
+----+--------+

该集中的某些用户ID在表中不存在。例如。 userID 5和userID 64不存在。

当我执行Select * from mytable where userID NOT IN (512,5,13,14,67)时,它将显示具有用户ID 651的行

+----+--------+
| Id | userID |
+----+--------+
|  4 |     51 |
|  5 |      6 |
+----+--------+

我想做类似的事情:
SELECT userID FROM my TABLE WHERE NOT EXISTS IN (1,5,10,15)

并得到结果:

+-------+--------+
|  Id   | userID |
+-------+--------+
| NULL  |      5 |
| NULL  |     64 |
+-------+--------+

我的表包含一百万行,搜索集可能包含1000个要搜索的ID。

1 个答案:

答案 0 :(得分:3)

如果我正确理解您的问题,那么您正在寻找。

  Row(
    children: <Widget>[
      Container(
        child: bubbleItem1,
        alignment: Alignment.centerLeft,
      ),
      Expanded(
        child: Divider(),
      ),
      Container(
        child: bubbleItem2,
        alignment: Alignment.centerRight,
      ) 
    ],
  )
  

搜索集可能是1000个ID,因此我无法执行类似的查询   那个。

然后,使用将由嵌套SELECT NULL AS id , search_filter.userID FROM ( SELECT 5 AS userID UNION SELECT 64 AS userID # [...] ) AS search_filter LEFT JOIN your_table ON search_filter.userID = your_table.userID WHERE your_table.userID IS NULL 函数解析的CSV搜索列表与SQL数字生成器结合使用是您唯一的也是最佳选择。

SUBSTRING_INDEX()

结果

SELECT 
   NULL AS id 
 , search_filter.userID
FROM (
  SELECT 
   DISTINCT 
    SUBSTRING_INDEX(
      SUBSTRING_INDEX(init_search.search_list, ',', sql_number_generator.number), ',', -1
    ) AS userID
FROM (

SELECT
  @number := @number + 1 AS number
FROM (
  (SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) row1
  CROSS JOIN
  (SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) row2
  CROSS JOIN
  (SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) row3 
  CROSS JOIN
  (SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) row4    
  CROSS JOIN 
  (SELECT @number:=0) AS init_user_params
)  
) AS sql_number_generator
CROSS JOIN (
 SElECT '512,5,13,14,67' AS search_list 
) AS init_search
) AS search_filter
LEFT JOIN
 your_table
ON
 search_filter.userID = your_table.userID
WHERE 
 your_table.userID IS NULL

请参阅demo