获取具有相同查询的数字限制的行数和行数

时间:2013-02-17 16:20:25

标签: mysql select count

假设在表A中,我有许多行,列值为a,b,c,d。

其中,我想要10行,列值为'c',同时还要计算列值为'c'的行总数。

我试过了:

select count(*),* from A where column like 'c'; (without limit)

select count(*),* from A where column like 'c' limit 10;

这甚至可能吗?或者我必须发出两个查询。

1 个答案:

答案 0 :(得分:1)

SELECT n.*, AA.cnt 
FROM nei_product_background_category c
    INNER JOIN nei_backgrounds n ON n.background_id = c.background_id
    INNER JOIN ( 
        SELECT cc.product_id, COUNT(*) AS cnt 
        FROM nei_product_background_category cc
                    INNER JOIN nei_backgrounds nn ON nn.background_id = cc.background_id
        GROUP BY cc.product_id
    ) AA ON c.product_id = AA.product_id
WHERE
    c.product_id = 578
LIMIT 10