简体   繁体   English

SQL查询具有多于一列重复一个值的行

[英]SQL query for rows having one value repeated in more then one column

I have table like: 我有这样的表:

在此处输入图片说明

I wanted to get all DeviceID which have key=1 more then 1 time. 我想获得所有具有key = 1的DeviceID,然后再进行1次。 Like device ID= 3 have two rows with key=1, same as 4. What is the query to get that result? 就像设备ID = 3一样,有两行键= 1,与4相同。要获得该结果的查询是什么?

I tried a lot but couldn't find any solution. 我尝试了很多,但是找不到任何解决方案。 It seems not possible with group by and having clause. group byhaving子句似乎不可能。

You should be able to use GROUP BY and HAVING to get the DeviceIds with more than one row: 您应该能够使用GROUP BYHAVING来获取具有多行的DeviceIds

select deviceid
from yourtable
where key = 1
group by deviceid
having count(key) > 1

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

Try something like this: 尝试这样的事情:

SELECT DeviceID
FROM YourTable 
WHERE key = 1
GROUP BY DeviceID
HAVING COUNT(*) > 1

Use this: 用这个:
SQL Fiddle SQL小提琴

SELECT DevID
FROM Devices
WHERE KeyID = 1
GROUP BY DevID
HAVING COUNT(*) > 1;

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

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