简体   繁体   English

选择记录,其中所有行在同一行的两列中具有相同的值

[英]Select records where all rows have same value in two columns in a single row

Here is my sample table 这是我的样品表

Col1 Col2
A      A
B      B
A      C
B      D
C      C

I want to be able to select distinct records where all rows have the same value in Col1 and Col2. 我希望能够选择在Col1和Col2中所有行都具有相同值的不同记录。 So my answer should be 所以我的答案应该是

Col1 Col2
A       A
B       B
C       C

Simply: 只是:

select distinct * from t where col1 = col2;

if both cols have null and you want to get that row too: 如果两个列都为null并且您也想获得该行:

select distinct * from t where coalesce(col1, col2) is null or col1 = col2;

The query is already written in your request: 该查询已写入您的请求中:

select distinct records where all rows have the same value in Col1 and Col2 选择所有记录 在Col1和Col2中具有相同值的 不同记录

SELECT DISTINCT *
FROM tbl
WHERE Col1 = Col2

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM