Mysql SELECT缩小搜索范围

Cud*_*dos 2 mysql select where

编辑:那很快.我之所以这样,是因为该表是两个表之间的数据透视表,其中一个表具有"id"作为主键,另一个表示"另一个"类型"主键

你好.

我想要以下内容:

找到只找到"id",其中"type"是1 AND 2 AND 3

这不起作用:

SELECT * FROM `table` WHERE `type` = 1 AND `type` = 2 AND `type` = 3;
Run Code Online (Sandbox Code Playgroud)

SELECT语句应该只返回一行(id = 1)

id type
1  1
1  2
1  3

2  1
2  2

3  3
Run Code Online (Sandbox Code Playgroud)

Cha*_*ana 6

如果你只是想知道ID然后添加关键字DISTINCT,只是选择ID,那里三个不同类型的记录...

  Select Distinct id 
  FROM `table` t
  WHERE  Exists (Select * From Table Where id = t.id and Type = '1')
     And Exists (Select * From Table Where id = t.id and Type = '2')
     And Exists (Select * From Table Where id = t.id and Type = '3')
Run Code Online (Sandbox Code Playgroud)

如果要查看Id和类型,请将类型添加到选择,

 Select Distinct id, Type 
  FROM `table` t
  WHERE  Exists (Select * From Table Where id = t.id and Type = '1')
     And Exists (Select * From Table Where id = t.id and Type = '2')
     And Exists (Select * From Table Where id = t.id and Type = '3')
Run Code Online (Sandbox Code Playgroud)

如果你想查看具有该id的每一行,那么请忽略不同的行

  Select id, Type 
  FROM `table` t
  WHERE  Exists (Select * From Table Where id = t.id and Type = '1')
     And Exists (Select * From Table Where id = t.id and Type = '2')
     And Exists (Select * From Table Where id = t.id and Type = '3')
Run Code Online (Sandbox Code Playgroud)