简体   繁体   English

Select条记录是唯一的

[英]Select records which is unique

I've a MySql table which contains rows like,我有一个 MySql 表,其中包含如下行,

pid进程号 country国家
436 436 1 1个
436 436 5 5个
436 436 7 7
439 439 7 7
440 440 3 3个
446 446 7 7
446 446 1 1个

For Example, I am trying to retrieve products which has only country 7 and should not present in other combination.例如,我正在尝试检索只有国家/地区 7 且不应出现在其他组合中的产品。 How can I achieve this?我怎样才能做到这一点?

I tried like below:我试过如下:

SELECT * from table_name WHERE country = 7;

but outcome/result contains pid 436,439 and 336. It should only give 439.但 outcome/result 包含 pid 436,439 和 336。它应该只给出 439。

You can use not exists for this:您可以为此使用不存在

select *
from table_name t 
where t.country = 7
and not exists (
  select * from table_name t2
  where t2.pid = t.pid and t2.pid != t.pid
);

We can use aggregation here:我们可以在这里使用聚合:

SELECT pid
FROM yourTable
GROUP BY pid
HAVING SUM(country <> 7) = 0;

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

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