mysql count 返回至少 1 行
我最近换了工作,从使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为 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.
原来我错过了一个小组:/
Turns out i was missing a group by :/