Jump to content

MYSQL Count nulls


dsp77

Recommended Posts

  • 3 weeks later...

lest say this table

CREATE TABLE IF NOT EXISTS `fishing` (
  `userid` int(11) NOT NULL,
  `fav_fish` varchar(255) DEFAULT NULL,
  `fav_lake` varchar(255) DEFAULT NULL,
  KEY `userid` (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

a user fills information in more tables with exactly the same structure (different names), some will be left null and at the end i want to find how many nulls are in x tables. I tried some query's with no success:( 

Link to comment
Share on other sites

Count rows where one column or the other (or both) is NULL

SELECT COUNT(userid) FROM fishing
WHERE fav_fish IS NULL
OR fav_lake IS NULL

 

Count rows where BOTH columns are NULL

SELECT COUNT(userid) FROM fishing
WHERE fav_fish IS NULL
AND fav_lake IS NULL

 

Of course, I don't think I would write the row if neither value was provided, so the query to count users who do not have a fishing record could be (depending on the name of your users table):

SELECT COUNT(users.userid)
FROM users LEFT JOIN fishing ON users.userid = fishing.userid
WHERE fishing.userid IS NULL

 

Link to comment
Share on other sites

I figured the userid would be unique. It would not be much use otherwise. The COUNT(userid) provides a count of the rows that satisfy the WHERE clause. You can use COUNT(*) if it makes you more comfortable. I prefer to count the primary key column because I believe it is more efficient. I have no proof that it is, so don't ask me to provide my sources. I just figure it is -- or can be.

 

If that is not what you were referring to, please elaborate on why you think my solution is not correct. Or how it failed when you tried it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.