gpt4 book ai didi

mysql - 在搜索特定 SQL 时计算整个数据库

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

我在数据库中有一张 table 供顾客及其眼镜使用

customer_inventory_tbl:

SELECT * FROM customer_inventory_tbl

+-------+-------+-------+
|id(pk) | name | spex |
+-------+-------+-------+
|1 |John |Oval |
|2 |Steve |Angular|
|3 |John |Aviator|
|4 |Kevin |Supra |
|5 |Jamie |Oval |
|6 |Ben |Supra |
+-------+-------+-------+

(这是一个更简化的版本,哈哈)

如果我查看约翰的记录,它会显示

SELECT * FROM customer_inventory_tbl WHERE name=John

+-------+-------+-------+
|id(pk) | name | spex |
+-------+-------+-------+
|1 |John |Oval |
|3 |John |Aviator|
+-------+-------+-------+

但是我需要的是在查看约翰的记录时,它向我展示

+-------+-------+-------+-----+
|id(pk) | name | spex |count|
+-------+-------+-------+-----+
|1 |John |Oval |2 |
|3 |John |Aviator|1 |
+-------+-------+-------+-----+

“计数”列是数据库中具有“Oval”示例的记录数。

现在这很容易了,如果我想计算数据库中的每条记录,但是如何在查找特定名称时获得所有记录的计数。

我希望这是有道理的

最佳答案

select c.*,
(
select count(1)
from customer_inventory_tbl
where spex = c.spex
) "count"
from customer_inventory_tbl c;

关于mysql - 在搜索特定 SQL 时计算整个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40962024/

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