获取 x 行数,其中 id = id(other table) 具有该 id 的行数最多
Getting x amount of rows where id = id(other table) with the highest amount of rows with that id
x = 无所谓
table一个
0 row0
1 row1
2 row2
table B
0 x
1 x
0 X
2 X
2 X
0 x
在此示例中有 3 行 0、2 行 2 和 1 行 1。
我想得到例如行数最多的两行。
期望的结果:
0 row0 ==> because 3 rows in b is the highest amount.
2 row2 ==> because 2 rows in b is the second highest amount.
我目前的尝试:
SELECT Id, Name FROM A
WHERE Id =
(
SELECT IdB FROM B
GROUP BY IdB
ORDER BY count(IdB) DESC
LIMIT 2
)
编辑:我使用 mysql
谢谢
不确定您想如何处理关系。
这样做是根据您的 ID 将两个 table 连接 (learn more about joins) 在一起,然后汇总计算 B.IDB 出现次数的结果,并显示该计数以及来自 table A
的名字
SELECT A.Name, count(B.IDB) cnt
FROM A
INNER JOIN IDB
on A.ID = B.IDB
GROUP BY A.Name
ORDER BY count(B.IDB) desc
LIMIT 2
但是对于你的例子,这应该 return
Name cnt
row0 3
row2 2
您想 JOIN
通过 id
:
将您的桌子放在一起
SELECT
A.id,
A.name
FROM
A join
B on A.id = B.id
GROUP BY
A.name
ORDER BY
count(B.id) desc
LIMIT 2
这将 return 输出与上面的示例相匹配。
-- 样本table
-- table 架构
create table A (id int, name varchar(10))
insert into A
values (0, 'row0')
insert into A
values (1, 'row1')
insert into A
values (2, 'row2')
select * from A
-- table 架构
create table B (id int, name varchar(10))
insert into B
values (0, 'X')
insert into B
values (0, 'X')
insert into B
values (0, 'X')
insert into B
values (0, 'X')
insert into B
values (1, 'X')
insert into B
values (2, 'X')
insert into B
values (2, 'X')
insert into B
values (2, 'X')
insert into B
values (2, 'X')
insert into B
values (2, 'X')
insert into B
values (2, 'X')
select * from B
--查询
select Top 2 A.name, count(B.id) from A
inner join B on A.id = b.id
group by A.name
order by count(A.name) desc
你可以试试;
select a.name, b.count
from A a,
(select idB, count(idB) count from B group by idB) b
where a.id = b.idB
and count > 1
x = 无所谓
table一个
0 row0
1 row1
2 row2
table B
0 x
1 x
0 X
2 X
2 X
0 x
在此示例中有 3 行 0、2 行 2 和 1 行 1。
我想得到例如行数最多的两行。
期望的结果:
0 row0 ==> because 3 rows in b is the highest amount.
2 row2 ==> because 2 rows in b is the second highest amount.
我目前的尝试:
SELECT Id, Name FROM A
WHERE Id =
(
SELECT IdB FROM B
GROUP BY IdB
ORDER BY count(IdB) DESC
LIMIT 2
)
编辑:我使用 mysql
谢谢
不确定您想如何处理关系。
这样做是根据您的 ID 将两个 table 连接 (learn more about joins) 在一起,然后汇总计算 B.IDB 出现次数的结果,并显示该计数以及来自 table A
的名字SELECT A.Name, count(B.IDB) cnt
FROM A
INNER JOIN IDB
on A.ID = B.IDB
GROUP BY A.Name
ORDER BY count(B.IDB) desc
LIMIT 2
但是对于你的例子,这应该 return
Name cnt
row0 3
row2 2
您想 JOIN
通过 id
:
SELECT
A.id,
A.name
FROM
A join
B on A.id = B.id
GROUP BY
A.name
ORDER BY
count(B.id) desc
LIMIT 2
这将 return 输出与上面的示例相匹配。
-- 样本table
-- table 架构
create table A (id int, name varchar(10))
insert into A
values (0, 'row0')
insert into A
values (1, 'row1')
insert into A
values (2, 'row2')
select * from A
-- table 架构
create table B (id int, name varchar(10))
insert into B
values (0, 'X')
insert into B
values (0, 'X')
insert into B
values (0, 'X')
insert into B
values (0, 'X')
insert into B
values (1, 'X')
insert into B
values (2, 'X')
insert into B
values (2, 'X')
insert into B
values (2, 'X')
insert into B
values (2, 'X')
insert into B
values (2, 'X')
insert into B
values (2, 'X')
select * from B
--查询
select Top 2 A.name, count(B.id) from A
inner join B on A.id = b.id
group by A.name
order by count(A.name) desc
你可以试试;
select a.name, b.count
from A a,
(select idB, count(idB) count from B group by idB) b
where a.id = b.idB
and count > 1