sql仅显示具有唯一值的行

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

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

  1. Product | Price
  2. Cheese | 5
  3. Yogurt | 2
  4. Milk | 2
  5. Soy Milk | 3

我希望结果是

  1. Cheese
  2. Soy Milk

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

e5nszbig

e5nszbig1#

你可以用 not exists :

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

或者,聚合:

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

3vpjnl9f2#

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

展开查看全部

相关问题