mysql count 返回至少 1 行

发布于 2024-11-29 17:49:38 字数 688 浏览 1 评论 0原文

我最近换了工作,从使用 T-SQL 转到 MySQL。到目前为止一切顺利直到今天。

我正在运行以下 sql 脚本:

 SELECT PB.idproductbundle AS ID, 
         PB.Name AS Name,
         PB.Discount AS Discount,
         PB.DiscountIsPercent AS DiscountIsPercent,  
         COUNT(PB_P.idproductbundle) AS ProductCount
 FROM `mydb`.productbundles AS PB
 LEFT JOIN `mydb`.ProductBundle_Product PB_P ON PB_P.idproductbundle = PB.idproductbundle

简单的命令,用于返回所有产品捆绑包,并统计该捆绑包中的产品数量。

奇怪的是,当前表中没有数据:productbundles 或 ProductBundle_Product。

但它坚持带回 1 行。所有列都是默认值:

ID Name Discount DiscountIsPercent ProductCount

NULL、NULL、NULL、NULL、'0'

在 T-Sql 中,这将没有行。

I've recently moved jobs and gone from a working with T-SQL to MySql. So far so good until today.

i'm running the following sql script:

 SELECT PB.idproductbundle AS ID, 
         PB.Name AS Name,
         PB.Discount AS Discount,
         PB.DiscountIsPercent AS DiscountIsPercent,  
         COUNT(PB_P.idproductbundle) AS ProductCount
 FROM `mydb`.productbundles AS PB
 LEFT JOIN `mydb`.ProductBundle_Product PB_P ON PB_P.idproductbundle = PB.idproductbundle

simple command to bring back all product bundles with a count of how many products in that bundle.

Strange thing is, there is currently no data in tables: productbundles or ProductBundle_Product.

but it insits on bringing back 1 row. all the columns are their default value:

ID Name Discount DiscountIsPercent ProductCount

NULL, NULL, NULL, NULL, '0'

In T-Sql this would have no rows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的笑 2024-12-06 17:49:38

因为 select 中有一个 COUNT 子句,如果没有满足查询的行,它将返回零。因此,您可以保证至少有一行 - COUNT 的结果告诉您有零行。

Because you have a COUNT clause in the select, which will bring back a zero if there are no rows that satisfy the query. So you're guaranteed at least one row - the result of the COUNT telling you there are zero rows.

盛装女皇 2024-12-06 17:49:38

原来我错过了一个小组:/

 SELECT PB.idproductbundle AS ID, 
         PB.Name AS Name,
         PB.Discount AS Discount,
         PB.DiscountIsPercent AS DiscountIsPercent,  
         COUNT(PB_P.idproductbundle) AS ProductCount
 FROM `ukiss-order-management`.productbundles AS PB
 LEFT JOIN `ukiss-order-management`.ProductBundle_Product PB_P ON PB_P.idproductbundle = PB.idproductbundle
 GROUP BY PB.idproductbundle

Turns out i was missing a group by :/

 SELECT PB.idproductbundle AS ID, 
         PB.Name AS Name,
         PB.Discount AS Discount,
         PB.DiscountIsPercent AS DiscountIsPercent,  
         COUNT(PB_P.idproductbundle) AS ProductCount
 FROM `ukiss-order-management`.productbundles AS PB
 LEFT JOIN `ukiss-order-management`.ProductBundle_Product PB_P ON PB_P.idproductbundle = PB.idproductbundle
 GROUP BY PB.idproductbundle
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文