简体   繁体   English

返回具有相同计数的所有值

[英]Return all values with same count

I have following query 我有以下查询

select id, nameOfPet, count(fed)
from petLover, pets
where id = 180 and petLover.nameOfPet = pets.nameOfPet
group by fed
order by fed desc;

So what the query does is get a person's id, get all the names of the pets he has, looks in the table pets for the same person and checks which pet has been fed how many times and output the person's id, the name of the pet and how often it has been fed. 所以查询的作用是获取一个人的身份,获取他拥有的宠物的所有名称,在同一个人的桌子上查看宠物并检查哪些宠物被喂了多少次并输出该人的身份证,宠物以及喂食的频率。

Now I want to only output the pet that has been fed the most. 现在我想只输出最多喂养的宠物。 I could certainly use limit 1 , but I want to output all, if the number of feeding is the same for several pets. 我当然可以使用limit 1 ,但我想输出所有,如果几个宠物的喂食数量是相同的。

The nested query derived the counts. 嵌套查询派生了计数。 Other than having only a single column it is identical to the outermost query. 除了只有一列,它与最外面的查询相同。

select id, nameOfPet, count(fed)
from petLover, pets
where id = 180 and petLover.nameOfPet = pets.nameOfPet
group by fed
having count(fed) >= ALL (
    select count(fed)
    from petLover, pets
    where id = 180 and petLover.nameOfPet = pets.nameOfPet
    group by fed
)

The query you have posted won't run; 您发布的查询将无法运行; you would need to group by id, nameOfPet. 你需要按id,nameOfPet进行分组。 This is personal preference, but I would also specify your join (to make it more readable and easier to change between types of join): 这是个人偏好,但我也会指定您的联接(以使其更易读,更容易在连接类型之间进行更改):

SELECT id, nameOfPet, COUNT(p.fed)
FROM petLover pl
LEFT OUTER JOIN pets p ON pl.nameOfPet = p.nameOfPet
WHERE id = 180
GROUP BY id, nameOfPet
ORDER BY COUNT(p.fed)

The LEFT OUTER JOIN will make sure you return all of the results from petLover, even if none have been fed (ie if none have been fed, you will return all petLovers). LEFT OUTER JOIN将确保您返回petLover的所有结果,即使没有被喂食(即如果没有喂食,您将返回所有petLovers)。 Change it back to an INNER JOIN if you only want results when an animal has been fed. 如果您只想在喂食动物时想要结果,请将其更改回INNER JOIN。 Here's a modified query to do what you're looking for (based upon rows): 这是一个修改过的查询来执行您要查找的内容(基于行):

SELECT pl.id, pl.nameOfPet, COUNT(*)
FROM petLover pl
LEFT OUTER JOIN pets p ON pl.nameOfPet = p.nameOfPet
GROUP BY pl.id, pl.nameOfPet
HAVING COUNT(*) >= ALL (
    SELECT COUNT(*)
    FROM petLover pl
    LEFT OUTER JOIN pets p ON pl.nameOfPet = p.nameOfPet
    GROUP BY pl.id, pl.nameOfPet
)
ORDER BY COUNT(*) DESC

EDIT 编辑

Further to the answer to my question in the original comments, you should be able to do the following to modify the SQL above: 除了原始注释中我的问题的答案之外,您应该能够执行以下操作来修改上面的SQL:

SELECT pl.id, pl.nameOfPet, SUM(p.fed)
FROM petLover pl
LEFT OUTER JOIN pets p ON pl.nameOfPet = p.nameOfPet
GROUP BY pl.id, pl.nameOfPet
HAVING SUM(p.fed) >= ALL (
    SELECT SUM(p.fed)
    FROM petLover pl
    LEFT OUTER JOIN pets p ON pl.nameOfPet = p.nameOfPet
    GROUP BY pl.id, pl.nameOfPet
)
ORDER BY SUM(p.fed) DESC

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

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