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
https://forums.phpfreaks.com/topic/243902-mysql-count-nulls/#findComment-1262611
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
https://forums.phpfreaks.com/topic/243902-mysql-count-nulls/#findComment-1262628
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
https://forums.phpfreaks.com/topic/243902-mysql-count-nulls/#findComment-1262636
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.