sql仅显示具有唯一值的行

bvn4nwqk  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(351)

我被一个愚蠢的新手问题弄糊涂了!我试图在sqlite中编写一个命令,只显示选定列中具有唯一值的行。
举个例子:

Product  | Price
Cheese   | 5
Yogurt   | 2
Milk     | 2
Soy Milk | 3

我希望结果是

Cheese
Soy Milk

我想答案很简单。有人能帮帮我吗?

e5nszbig

e5nszbig1#

你可以用 not exists :

select t.*
from t
where not exists (select 1 from t t2 where t2.price = t.price and t2.product <> t.product);

或者,聚合:

select max(product)
from t
group by price
having count(*) = 1;
3vpjnl9f

3vpjnl9f2#

使用以下命令尝试 IN ```
select
product
from yourTable
where price not in
(
select
price
from yourTable
group by
price
having count(*) > 1
)

相关问题