简体   繁体   English

MySQL根据特定的列值获取所有行(按列过滤)

[英]MySQL get all rows based on a specific column value (filter by column)

I have this table A here: 我在这里有此表A:

| colA | colB
| 1    | 2
| 2    | 2 
| 3    | 3
| 3    | 2

I want to query all rows where colB is both 3 and 2. In which the result would be: 我想查询colB为3和2的所有行。结果将是:

|colA  | colB
| 3    | 3
| 3    | 2

I tried querying: 我尝试查询:

select * from A where colB = 2 AND colB= 3

But I keep getting an empty response. 但是我一直都没有回应。 Tried OR but it also won't work. 尝试过OR但也无法正常工作。 I understand that what I experimented is sort of a "row" filtering. 我了解到,我尝试过的是某种“行”过滤。

Is there a way to handle "column" filtering? 有没有办法处理“列”过滤?

Try this: 尝试这个:

select *
from A
where colA IN (select colA 
               from A 
               where colB IN (2, 3)
               group by colA
               having count(distinct colB) = 2)

or with a JOIN : 或使用JOIN

select A.*
from A 
join (select colA 
      from A 
      where colB IN (2, 3)
      group by colA
      having count(distinct colB) = 2
) as t on A.colA = t.ColA

You can use EXISTS() : 您可以使用EXISTS()

SELECT * FROM YourTable a
WHERE EXISTS(SELECT 1 from YourTable s
             WHERE a.colA = s.colA
              and s.colB in(2,3)
             GROUP BY s.colA having count(distinct s.colB) = 2)

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

相关问题 显示MySQL中包含相同值的所有行(2列过滤器) - Show all rows in MySQL that contain the same value (2 column filter) PHP / MySQL:在列中显示与特定值匹配的所有行 - PHP / MySQL: Display all rows matching specific value in column MySQL-如何获取特定“ id”内一列中所有行的差值? - MySQL - How to get the difference of all the rows in a column within a specific “id”? mysql中根据特定列数据过滤数据 - Filter data based on specific column data in mysql 根据特定列/字段的随机值获取记录-MySQL - Get Records based on a Random value for a specific column/field - MySQL MySQL过滤多行中的列值? - MySQL filter on column value within multiple rows? MySQL查询:当另一列中的值符合特定条件时,返回一列中具有特定值的所有行 - MySQL Query: Return all rows with a certain value in one column when value in another column matches specific criteria 根据过滤器使用sqlalchamey选择特定的列值 - Select specific column value with sqlalchamey based on filter MySQL-获取列的所有唯一值,检查是否具有特定值 - MySQL - get all unique values of a column, check if has a specific value MySQL语句从所有行获取列的值,其中另一列的值=指定的值 - MySQL statement to get a column's value from all rows where another column's value = a specified value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM