count() for a select statement?

Started by Jean-Christian Imbeaultover 23 years ago3 messagesgeneral
Jump to latest
#1Jean-Christian Imbeault
jc@mega-bucks.co.jp

Is there an efficient way to get a count of the number of rows returned
by this kind of query?

select id from products where name ILIKE 'TNT' UNION select id from
products where name ILIKE 'ATOM' UNION select id from products where
name ILIKE 'BOB'

I don't need any of the data returned, just a row count. The way I do it
now is to execute the query and use (in PHP) pg_numrows() on the result
set. But that mean the whole result set is being sent back to me when
all I need is a count ...

I've tried select count( select id from ...) but that didn't work ;)

Jc

#2Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Jean-Christian Imbeault (#1)
Re: count() for a select statement?

On Wed, 2 Oct 2002, Jean-Christian Imbeault wrote:

Is there an efficient way to get a count of the number of rows returned
by this kind of query?

select id from products where name ILIKE 'TNT' UNION select id from
products where name ILIKE 'ATOM' UNION select id from products where
name ILIKE 'BOB'

Probably "select count(*) from (select id from ... ) as t"
should work.

#3snpe
snpe@snpe.co.yu
In reply to: Jean-Christian Imbeault (#1)
Re: count() for a select statement?

On Tuesday 01 October 2002 07:46 pm, Jean-Christian Imbeault wrote:

Is there an efficient way to get a count of the number of rows returned
by this kind of query?

select id from products where name ILIKE 'TNT' UNION select id from
products where name ILIKE 'ATOM' UNION select id from products where
name ILIKE 'BOB'

I don't need any of the data returned, just a row count. The way I do it
now is to execute the query and use (in PHP) pg_numrows() on the result
set. But that mean the whole result set is being sent back to me when
all I need is a count ...

I've tried select count( select id from ...) but that didn't work ;)

You try :
select count(*)
from (select id from ...)

regards
haris peco