Dan Hennion
Dan Hennion

Reputation: 1745

Get number of rows returned in MySQL query

I'm having some trouble with a MySQL query I'm writing. I'd like to get a count of the number of rows that my query is returning without actually returning the rows and then using mysql_num_rows or the like.

My query is as follows:

SELECT COUNT(l.product_number_language) as counts, l.id, l.product_number, l.language,    l.product_number_language
FROM bs_products_languages l
LEFT JOIN bs_products p ON (l.product_number_language = p.product_number)
WHERE l.product_number = 'C4164' 
AND l.active='Y'
AND p.active='Y'
GROUP BY l.language

The following is what is being returned: Screenshot

And what I really want is simply a count of these rows, so in this case 3.

Upvotes: 17

Views: 30979

Answers (1)

Andy Refuerzo
Andy Refuerzo

Reputation: 3332

Select count(*)
From
(
    SELECT COUNT(l.product_number_language) as counts, l.id, l.product_number, 
        l.language, l.product_number_language
    FROM bs_products_languages l
    LEFT JOIN bs_products p ON (l.product_number_language = p.product_number)
    WHERE l.product_number = 'C4164' 
    AND l.active='Y'
    AND p.active='Y'
    GROUP BY l.language
) as t

Upvotes: 41

Related Questions