简体   繁体   English

sql - 在另一列中选择具有相同ID但不同值的行

[英]sql-Select rows with same id but different value in another column

Input:输入:

Column A A列 Column B B列
1 1个 A一种
1 1个 B
1 1个 C C
2 2个 A一种
2 2个 B
3 3个 A一种
3 3个 B
3 3个 c c
4 4个 A一种
5 5个 A一种
5 5个 B
6 6个 A一种

Output Output

ColumnA列A
4 4个
6. 6.

for columnA for every first of values is A, i need to fetch columnA those values have only A not in B,C对于 columnA,每个第一个值都是 A,我需要获取 columnA 这些值只有 A 而不是 B,C

You can use a correlated subquery with NOT EXISTS to exclude column A values that have matching rows with the disallowed values in column B.您可以使用带NOT EXISTS的相关子查询来排除列 A 值,这些值具有与列 B 中不允许的值匹配的行。

SELECT columnA
FROM yourTable AS t1
WHERE columnB = 'A'
AND NOT EXISTS (
    SELECT *
    FROM yourTable AS t2
    WHERE t1.columnA = t2.columnA
        AND t2.columnB IN ('B', 'C')
)

暂无
暂无

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

相关问题 MySQL选择具有相同id且在列中具有不同值的不同行,并在另一列中排除特定值 - MySQL Select distinct rows with same id with different value in a column and excluded a specific value in another column 从表中选择行,其中具有相同id的另一个表中的行在另一列中具有特定值 - Select rows from a table where row in another table with same id has a particular value in another column mysql,在另一列中选择两个具有相同id但值不同的不同行 - mysql, select two different rows with same id but different values in another column 按列ID选择行,如果包含null,则包含另一列中包含相同值的行 - Select row by column id AND include rows that contain the same value from another column if null SQL选择具有相同列值的行 - SQL select rows with the same column's value sql:使用单个查询选择另一列中具有相同列值的行 - sql: select rows which have the same column value in another column with a single query 如何选择特殊列与另一行的值不同的SQL行? - How to select SQL rows where a spesific column is not the same value as another row? MySQL-在另一列上获取具有相同ID但特定值不同的行 - MySQL - get rows with same ID but different specific values on another column SQL 合并两行ID相同但列值不同的行 - SQL Merge two rows with same ID but different column values SQL 更多行 ID 相同但列不同 - SQL More rows with the same ID but one different column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM