Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to count rows?

Status
Not open for further replies.

milton747

Programmer
Apr 21, 2005
133
US
I have working SQL to count number of rows returned.
When I insert a WHERE clause for VT(Vermont), the correct 4 rows are returned, but the row count is still a huge number. (I was expecting the count to be 4 to match the 4 rows returned.)

Can anyone suggest SQL to count number of rows returned AFTER the WHERE clause is applied?
Thanks. Milt.

SELECT
brnNumber AS brnNumber, brnCity AS brnCity, x.rc
FROM branchlocations,
(
SELECT
count(*) AS rc
FROM branchlocations
)
AS x
WHERE brnState = 'VT'

----------------------------------
Simply delete the WHERE line and all rows are returned. But the row count is always the same...the entire table, about 800.
-----------------------------------



 
SOLUTION.... Include where in count sub-query. Sorry.

SELECT
brnNumber AS brnNumber, brnCity AS brnCity, x.rc
FROM branchlocations,
(
SELECT
count(*) AS rc
FROM branchlocations
WHERE brnState = 'VT'
)
AS x
WHERE brnState = 'VT'
 
just curious...

where are you using the results of this query?

if it's in an application language like php, you needn't burden mysql with the extra query to get the count -- just use php's mysql_num_rows function right after executing the query, and it'll tell you how many rows were returned

other application languages have similar functions

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top