我有一张SN和标题表 在我做的第一个查询中:
create or replace view AB as
select wrote.serial_number as SN, count(wrote.serial_number) as NumOfPapers
from wrote
group by wrote.serial_number;
我得到一个SN的视图和每个SN的标题数 比我想要获得最多标题(最大计数)的行:ID | MAX(计数) 所以我这样做:
select AB.SN, max(AB.NumOfPapers)
from AB
但是我获得了第一行人的SN的最大计数(不是具有最多标题的真实SN)
我必须使用max()...感谢使用'order by'的建议,但它们与此无关
我做错了什么?
谢谢!
答案 0 :(得分:1)
我想你想要这样的东西:
SELECT sn
FROM ab
ORDER BY NumOfPapers DESC
LIMIT 1
答案 1 :(得分:1)
你只得到第一行NumOfPapers的原因是因为你也在选择ab.serial数字。所以,我认为这会给你你想要的东西:
select AB.SN, AB.NumOfPapers FROM AB order by AB.NumOfPapers desc limit 1;
答案 2 :(得分:0)
create or replace view AB as
select wrote.serial_number as SN, count(wrote.serial_number) as NumOfPapers
from wrote
group by wrote.serial_number
ORDER by NumOfPapers DESC;
和
select AB.SN, AB.NumOfPapers from AB LIMIT 1
答案 3 :(得分:0)
您使用的NumOfPapers
计数为serial_number
select AB.SN, max(wrote.serial_number)
from AB, wrote
答案 4 :(得分:0)
我想我弄明白了:
create or replace view AB as
select wrote.serial_number as SN, count(wrote.serial_number) as NumOfPapers
from wrote
group by wrote.serial_number;
select AB.SN, AB.NumOfPapers
from AB
where AB.NumOfPapers = (select max(AB.NumOfPapers)
from AB)
谢谢大家的帮助,你真的指导我了!
答案 5 :(得分:-1)
我相信你的第二个查询会向你的数据库询问AB和最大的每个AB.SN(AB.NumOfPapers) 你尝试过where语句吗?
select AB.SN, max(AB.NumOfPapers)
from AB
where AB.NumOfPapers = max(AB.NumOfPapers)
(我没有对此进行测试,可能需要进行一些调整)