MySQL Count

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

I'm not at a place where I can test this code out, but I was wondering how the count function of MySQL works.

Does it neglect NULL rows?

For example, I want to count the total number of rows in a table that have a value for some field.

Normally I would just do soemthing like

SQL Code: [ Select ]

Now would the below code return the same number or would it return the total rows? Does count take into account if the field is null or not.

SQL Code: [ Select ]

If so, which is faster ;)

When I get home tonigth I think I'll test this out if no one else has come up with an answer.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8431
  • Loc: USA

Post 3+ Months Ago

From what I discovered from Google, count(val) doesn't count null values while count(*) counts the null values... this makes count(val) slower though and the SQL Optimizer does a better job with count(*) with a where clause.

Source: Yes, it was written in 2007. Not sure if any MySQL updates changed that but... that is what I could find out without actually testing anything (I don't have the time... )

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 85 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2015. Ozzu® is a registered trademark of Unmelted, LLC.