如何对一行进行分组和计数?

发布于 2024-10-24 05:47:10 字数 1150 浏览 4 评论 0原文

为了简化,我有树表:productsproducts-vs-ordersorders

  1. products 字段:'ProductID ', 'Name', 'isGratis', ...
  2. products-vs-orders 字段:'ProductID'、'OrderID'
  3. orders 字段:'OrderID'、'Title ',...

实际上,我有一个这样的查询:

SELECT orders.OrderID, orders.Title, COUNT(`products`.`isGratis`) AS "Quantity", `products`.`isGratis`
FROM `orders`, `products-vs-orders`, `products`
WHERE `orders`.`OrderID` = `products-vs-orders`.`OrderID` AND `products-vs-orders`.`ProductID` = `products`.`ProductID`
GROUP BY `products`.`PackID`, `products`.`isGratis`

此查询有效并返回结果:

OrderID,    Title,      Quantity,   isGratis
1            My Order      20           0
1            My Order      3            1
2            An other      8            0
2            An other      1            1

如何将产品“免费”和“付费”的计数检索到单独的列中?

OrderID,    Title,      Qt Paid,        Qt Gratis
1            My Order       20              3
2            An other       8               1

To simplify, I have tree tables: products, products-vs-orders, orders

  1. products fields : 'ProductID', 'Name', 'isGratis', ...
  2. products-vs-orders fields : 'ProductID', 'OrderID'
  3. orders fields : 'OrderID', 'Title', ...

Actually, I have a query like this:

SELECT orders.OrderID, orders.Title, COUNT(`products`.`isGratis`) AS "Quantity", `products`.`isGratis`
FROM `orders`, `products-vs-orders`, `products`
WHERE `orders`.`OrderID` = `products-vs-orders`.`OrderID` AND `products-vs-orders`.`ProductID` = `products`.`ProductID`
GROUP BY `products`.`PackID`, `products`.`isGratis`

This query works and return this surch of result:

OrderID,    Title,      Quantity,   isGratis
1            My Order      20           0
1            My Order      3            1
2            An other      8            0
2            An other      1            1

How can I retrieve the count of products 'gratis' and 'paid' in to separate cols ?

OrderID,    Title,      Qt Paid,        Qt Gratis
1            My Order       20              3
2            An other       8               1

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

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

发布评论

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

评论(3

不美如何 2024-10-31 05:47:10

试试这个:

SELECT
    orders.OrderID,
    orders.Title, 
    COUNT(orders.OrderId) - SUM(`products`.`isGratis`) AS "Qt Paid", 
    SUM(`products`.`isGratis`) AS "Qt Gratis" 
WHERE `orders`.`OrderID` = `products-vs-orders`.`OrderID`
  AND `products-vs-orders`.`ProductID` = `products`.`ProductID`
GROUP BY `products`.`PackID`

Try this:

SELECT
    orders.OrderID,
    orders.Title, 
    COUNT(orders.OrderId) - SUM(`products`.`isGratis`) AS "Qt Paid", 
    SUM(`products`.`isGratis`) AS "Qt Gratis" 
WHERE `orders`.`OrderID` = `products-vs-orders`.`OrderID`
  AND `products-vs-orders`.`ProductID` = `products`.`ProductID`
GROUP BY `products`.`PackID`
暮倦 2024-10-31 05:47:10

SUM(products.isGratis) 取决于数据库在内部将布尔值表示为单个数字位的事实,因此 false = 0 且 true = 1。ALL

中的情况可能并非如此数据库实现。因此,对布尔字段进行 SUM 可能会导致依赖于实现的行为。

在求和之前将布尔值转换为实际的 0 和 1 值应该更合适:

SELECT orders.OrderID, orders.Title, 
  SUM(CASE WHEN products.isGratis THEN 0 ELSE 1 END) AS "Qt Paid",
  SUM(CASE WHEN products.isGratis THEN 1 ELSE 0 END) AS "Qt Gratis"
FROM orders INNER JOIN `products-vs-orders` ON (orders.OrderID = `products-vs-orders`.OrderID)
  INNER JOIN products ON (`products-vs-orders`.ProductID = products.ProductID)
GROUP BY orders.OrderID, orders.Title

SUM(products.isGratis) depends on the fact that a boolean value is internally represented by the database as a single numeric bit, so false = 0 and true = 1.

This may not be the case in ALL DB implementations. Therefore, SUM over a boolean field may cause implementation-dependent behavior.

Converting the boolean into actual 0 and 1 values before summing should be more proper:

SELECT orders.OrderID, orders.Title, 
  SUM(CASE WHEN products.isGratis THEN 0 ELSE 1 END) AS "Qt Paid",
  SUM(CASE WHEN products.isGratis THEN 1 ELSE 0 END) AS "Qt Gratis"
FROM orders INNER JOIN `products-vs-orders` ON (orders.OrderID = `products-vs-orders`.OrderID)
  INNER JOIN products ON (`products-vs-orders`.ProductID = products.ProductID)
GROUP BY orders.OrderID, orders.Title
幼儿园老大 2024-10-31 05:47:10
select orderid,title,sum(if(isgratis=0,quantity,0)) as paid,sum(if(isgratis=1,quantity,0)) as gratis from ...
select orderid,title,sum(if(isgratis=0,quantity,0)) as paid,sum(if(isgratis=1,quantity,0)) as gratis from ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文