![](/img/trans.png)
[英]How to compare the column values of two last inserted specified rows on the same table?
[英]How to compare column values/rows in same table
我相信你想要:
select user_id
from t
group by user_id
having count(*) = count(distinct firstname) and
count(*) = count(distinct lastname);
我不確定您是否要將名稱配對。 如果是這樣的話:
having count(*) = count(distinct firstname, lastname)
並非所有數據庫都支持count(distinct)
多個參數。 如果這是您的真正意圖,那么不使用此短語就很容易了。
select user_id,count(*)
from table
group by user_id having
count(distinct firstname) =count(firstname) and count(distinct
lastname)=count(lastname) ;
如果每個用戶標識組的不同的fname和lname大於1,則表示這些用戶標識在分組用戶標識的位置上,這意味着該組不包含重復項。
遵循SQL將會起作用-如果您可以將這一行添加到示例數據中(8,7,'will','Rj'),則也將起作用,從而使User_id 7具有4行,但其中兩行具有相同的名稱。
with temp as (
SELECT user_id, firstname, lastname
, count(*) over (partition by user_id, firstname, lastname) as counter
FROM t2
)
SELECT * --distinct user_id
FROM temp t
WHERE not exists (SELECT 1 FROM temp WHEE counter = 2 and user_id = t.user_id)
該語句返回所有具有名稱的行,除非您將*更改為帶注釋的不同的user_id,否則它將僅返回僅具有唯一名稱的user_id。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.