簡體   English   中英

查詢以匹配一列中具有不同值的不同值

[英]Query to match different values in one column that have same value in a separate column

我正在嘗試編寫一個查詢,該查詢將在b列中使用兩個不同的值,然后將其與c列進行比較,以確定b列中的兩個值是否在c列中共享相同的值。但是我在輸出中也需要a列好。

例如

Column A         Column B        Column C
Test 1           x               12345
Test 2           y               12345
Test 3`          A               12344
Test 4           D               12342

期望的輸出

Column A        Column B        Column C
Test 1           x              12345
Test 2           y              12345

任何幫助都會很棒

我不確定ColumnB中的值是否有意義。 此查詢查找重復的ColumnC值,然后返回這些行:

select * from T where ColumnC in (
    select ColumnC from T
    group by ColumnC
    having count(*) > 1 /* or maybe count(distinct ColumnB) > 1 */
)

您可以按以下方式執行JOIN 觀看演示小提琴http://sqlfiddle.com/#!9/da525/4

select t.*
from tbl1 t join tbl1 s on t.`Column C` = s.`Column C`
and t.`Column B` <> s.`Column B`;

(或)使用WHERE EXISTS

select t.*
from tbl1 t
where exists ( select 1 from tbl1 
              where `Column C` = t.`Column C`
              and `Column B` <> t.`Column B`);

嘗試這個

SELECT a.* FROM table a join table b on a.c=b.c and a.b<>b.b

該查詢不考慮在c和b列中具有相同值的行。 如果需要,可以在選擇中添加DISTINCT

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM