我有这个查询
MYSQL:
SELECT COUNT(op.quantity) AS quantity_sold, op.model AS model_number,
op.name AS product_name, ptc.category_id, cd.name
FROM ((`order_product` op INNER JOIN `product` p ON op.product_id = p.product_id)
INNER JOIN (`product_to_category` ptc
INNER JOIN `category_description` cd ON ptc.category_id = cd.category_id) ON op.product_id = ptc.product_id)
INNER JOIN `order` o on o.order_id = op.order_id GROUP BY op.model ORDER BY quantity_sold DESC;
我尝试使用此代码来计算否。上述结果的行数。
SELECT COUNT(*) AS total FROM ((`order_product` op INNER JOIN `product` p ON op.product_id = p.product_id)
INNER JOIN (`product_to_category` ptc
INNER JOIN `category_description` cd ON ptc.category_id = cd.category_id) ON op.product_id = ptc.product_id)
INNER JOIN `order` o on o.order_id = op.order_id GROUP BY op.model;
但我无法获得计数单值。
为了分页,我需要将第一个查询返回的行数作为单个值。
答案 0 :(得分:0)
如果您想要查询返回的行计数,将其包装在外部查询中应该可以提供您想要的内容:
SELECT COUNT(*) AS row_count FROM (
SELECT
COUNT(op.quantity) AS quantity_sold,
op.model AS model_number,
op.name AS product_name,
ptc.category_id,
cd.name
FROM order_product op
INNER JOIN product p ON op.product_id = p.product_id
INNER JOIN product_to_category ptc ON op.product_id = ptc.product_id
INNER JOIN category_description cd ON ptc.category_id = cd.category_id
INNER JOIN `order` o on o.order_id = op.order_id
GROUP BY op.model
ORDER BY quantity_sold DESC
) A;