gpt4 book ai didi

c# - SQL 计数不符合我想要的结果

转载 作者:太空宇宙 更新时间:2023-11-03 20:10:04 25 4
gpt4 key购买 nike

我有两个不同的表 Products 和 ProductImages:

SELECT COUNT(p.productid) AS product_count, COUNT(pi.imageid) AS image_count  
FROM [Products] p, [ProductImages] pi
WHERE p.productid=126 AND pi.productid=126;

以上查询为我提供了每个 COUNT 中两个计数的乘积。请指导我哪里错了,我可以用单选查询来做吗

最佳答案

你可以把它分成两行

SELECT COUNT(p.productid) As Count
FROM [Products] p
WHERE p.productid=126
UNION All
SELECT COUNT(pi.imageid) As Count
FROM [ProductImages] pi
WHERE pi.productid=126;

或者您可以在没有 UNION ALL

的情况下执行此操作
SELECT 
(SELECT COUNT(p.productid) As Count
From [Products] p WHERE p.productid=126) ) as t1,
(SELECT COUNT(pi.imageid) As Count
FROM [ProductImages] pi WHERE pi.productid=126) as t2

关于c# - SQL 计数不符合我想要的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20605679/

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