Dudo1985 Dudo1985 - 1 year ago 85
SQL Question

Does COUNT() run twice?

I've this code:

"SELECT post_id, COUNT(post_id) AS number_of_votes, (SUM(vote) / COUNT(post_id)) AS result
FROM " . LOG_TABLE . " , $wpdb->posts AS p
WHERE post_id = p.ID
AND p.post_status = 'publish'
GROUP BY post_id
HAVING COUNT(post_id) >= 2
ORDER BY result DESC, COUNT(post_id) DESC

I'd like to know if the COUNT used in the first row run twice?

EDIT: for run twice I mean if it acces the table twice

Best Regards,

Answer Source

unless you are using subqueries of the below form,Table will be accessed only once(if it is what you meant by count, will be accessed twice),so count will be calculated only once and used everywhere

select id,(select min(id) from table1 t2 where t1.id=t2.id)b
table1 t1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download