gpt4 book ai didi

mysql - 如何计算(SQL)中的行数?

转载 作者:行者123 更新时间:2023-11-29 07:32:52 25 4
gpt4 key购买 nike

我有两个表 A、B,我的查询显示它:(连接 A 和 B)

select A.i,A.j,B.x,B.y,B.z from A inner join B on A.id=B.id_B order by A.i,A.j

.

+-----+-----+-----+-----+-----+
| A.i | A.j | B.x | B.y | B.z |
+=====+=====+=====+=====+=====+
| bk | bg | 1 | inf1| bar |
+-----+-----+-----+-----+-----+
| bk | bg | 2 | inf2| bar |
+-----+-----+-----+-----+-----+
| bk | bg | 3 | inf3| y |
+-----+-----+-----+-----+-----+
| ro | fn | 5 | enf1| bar |
+-----+-----+-----+-----+-----+
| ro | fn | 3 | enf2| bar |
+-----+-----+-----+-----+-----+
| st | st | 3 | onf1| bar |
+-----+-----+-----+-----+-----+

现在我想知道是多少次

A.i

像这样重复:

    +-----+-----+-----+-----+-----+
| A.i | A.j | B.x | B.y | B.z |RepeatColumn
+=====+=====+=====+=====+=====+---
| bk | bg | 1 | inf1| bar | 3 |(bk is repeated 3 times)
+-----+-----+-----+-----+-----+---
| bk | bg | 2 | inf2| bar | 3 |(bk is repeated 3 times)
+-----+-----+-----+-----+-----+---
| bk | bg | 3 | inf3| y | 3 |(bk is repeated 3 times)
+-----+-----+-----+-----+-----+---
| ro | fn | 5 | enf1| bar | 2 |(ro is repeated 2 times)
+-----+-----+-----+-----+-----+---
| ro | fn | 3 | enf2| bar | 2 |(ro is repeated 2 times)
+-----+-----+-----+-----+-----+---
| st | st | 3 | onf1| bar | 1 |(st is repeated once)
+-----+-----+-----+-----+-----+---

我怎样才能做那个查询,有人知道吗?

最佳答案

你需要子查询:

select A.i,A.j,
B.x,B.y,B.z,
(select count(*) from b where b.id = a.id) RepeatColumn
from A
inner join B
order by A.i,A.j;

但是,我没有在 o/p 中找到表(AB)之间的任何关系。所以,我只是在飞行中使用。

关于mysql - 如何计算(SQL)中的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50356473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com