具有相同字段值的行数

时间:2017-05-17 16:55:51

标签: mysql

我有查询

SELECT
    salesorderid,
    integration_betapost.goods_to_products.shipping_order_row_good_id,
    vtiger_inventoryproductrel.quantity
FROM
    vtiger_salesorder
INNER JOIN vtiger_inventoryproductrel ON vtiger_salesorder.salesorderid = vtiger_inventoryproductrel.id
INNER JOIN vtiger_products ON vtiger_inventoryproductrel.productid = vtiger_products.productid
INNER JOIN integration_betapost.goods_to_products ON vtiger_products.productid = integration_betapost.goods_to_products.productid
WHERE
    sostatus = 'Отправлять'

得到这个result 如何计算具有相同salesorderid的行数和具有相同salesorderidshipping_order_row_good_id的行数?

1 个答案:

答案 0 :(得分:3)

在查询中添加GROUP BY子句

SELECT
    salesorderid,
    integration_betapost.goods_to_products.shipping_order_row_good_id,
    COUNT(Quantity)
FROM
    vtiger_salesorder
INNER JOIN vtiger_inventoryproductrel ON vtiger_salesorder.salesorderid = vtiger_inventoryproductrel.id
INNER JOIN vtiger_products ON vtiger_inventoryproductrel.productid = vtiger_products.productid
INNER JOIN integration_betapost.goods_to_products ON vtiger_products.productid = integration_betapost.goods_to_products.productid
WHERE
    sostatus = 'Отправлять'
GROUP BY
    salesorderid
    ,integration_betapost.goods_to_products.shipping_order_row_good_id

如果您想对每个salesorderintegration_betapost.goods_to_products.shipping_order_row_good_id的数量求和,可以更改COUNT的{​​{1}}。