View Full Version : COUNT(table.col) problem
tosbourn
11-14-2008, 10:55 AM
I recently (about 10 minutes ago!) discovered that COUNT(*) is WAY more efficient than COUNT(col) - for some unknown reason I wrongly assumed the opposite would be true... ANYWAY, I am going through some queries I have made in the past and changing them where I can.
When it is just a simple COUNT(id) I can replace it with COUNT(*) fine, but when I need to do COUNT(table.id) it won't let me simply put COUNT (table.*), so what is the equivalent?
Cheers for any help!
abduraooft
11-14-2008, 11:30 AM
but when I need to do COUNT(table.id) it won't let me simply put COUNT (table.*), so what is the equivalent?
Select count(*) as someCount from table
tosbourn
11-14-2008, 12:03 PM
So even if I have...
SELECT COUNT(*) AS count FROM table1 AS t1, table2 AS t2....
It will count from the first table listed after the FROM?
Fumigator
11-14-2008, 06:34 PM
Remember what specifying a column name in a COUNT() function does. It counts the rows that will be returned where the column value is not null. So COUNT(table1.id) in a JOIN query simply counts all rows in the result of that join where that column id is not null.
In other words, there's no point in prefixing the * with a table name-- the count isn't going to single out rows from one table in the join, it is going to count the results of the join.
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.