Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 609825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:36:29+00:00 2026-05-13T17:36:29+00:00

Just wondering, is there any quick way to count all the NULL values (from

  • 0

Just wondering, is there any quick way to count all the NULL values (from all columns) in a MySQL table?

Thanks for any idea!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:36:29+00:00Added an answer on May 13, 2026 at 5:36 pm

    If you want this done exclusively by MYSQL and without enumerating all of the columns take a look at this solution.

    In this method you don’t have to maintain the number of database columns by hard coding them. If your table schema will get modified this method will work, and won’t require code change.

    SET @db = 'testing'; -- database
    SET @tb = 'fuzzysearch'; -- table
    SET @x = ''; -- will hold the column names with ASCII method applied to retrieve the number of the first char
    SET @numcolumns = 0; -- will hold the number of columns in the table
    
    -- figure out how many columns we have
    SELECT count(*) into @numcolumns FROM information_schema.columns where table_name=@tb and table_schema=@db;
    
    -- we have to prepare some query from all columns of the table
    SELECT group_concat(CONCAT('ASCII(',column_name,')') SEPARATOR ",") into @x from information_schema.columns where table_name=@tb and table_schema=@db;
    -- after this query we have a variable separated with comma like
    -- ASCII(col1),ASCII(col2),ASCII(col3)
    
    -- we now generate a query to concat the columns using comma as separator (null values are omitted from concat)
    -- then figgure out how many times the comma is in that substring (this is done by using length(value)-length(replace(value,',',''))
    -- the number returned is how many non null columns we have in that column
    -- then we deduct the number from the known number of columns, calculated previously
    -- the +1 is added because there is no comma for single value
    SET @s = CONCAT('SELECT @numcolumns - (length(CONCAT_WS(\',\',', @x, '))-length(replace(CONCAT_WS(\',\',', @x, '),\',\',\'\')) + 1) FROM ',@db,'.',@tb,';');
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    -- after this execution we have returned for each row the number of null columns
    -- I will leave to you to add a sum() group call if you want to find the null values for the whole table
    DEALLOCATE PREPARE stmt;
    

    The ASCII is used to avoid reading, concatenating very long columns for nothing, also ASCII makes us safe for values where the first char is a comma(,).

    Since you are working with reports, you may find this helpful as this can be reused for each table if you put in a method.

    I tried to let as many comments as possible.

    Let’s split on pieces the above compact way (reverse way):

    I wanted to end up having a query like this

    SELECT totalcolumns - notnullcolumns from table; -- to return null columns for each row
    

    While the first one is easy to calcule by running:

    SELECT count(*) FROM information_schema.columns where table_name=@tb and table_schema=@db;
    

    The second one the notnullcolumns is a bit of pain.
    After a piece of examination of the functions available in MySQL, we detect that CONCAT_WS does not CONCAT null values

    So running a query like this:

    SELECT CONCAT_WS(",","First name",NULL,"Last Name");
    returns: 'First name,Last Name'
    

    This is good, we take rid of the null values from the enumeration.
    But how do we get how many columns were actually concatenated?

    Well that is tricky. We have to calculate the number of commas+1 to get the actually concatenated columns.

    For this trick we used the following SQL notation

    select length(value)-length(replace(value,',','')) +1 from table
    

    Ok, so we have now the number of concatenated columns.

    But the harder part is coming next.

    We have to enumerate for CONCAT_WS() all values.
    We need to have something like this:

    SELECT CONCAT_WS(",",col1,col2,col3,col4,col5);
    

    This is where we have to take use of the prepared statements, as we have to prepare an SQL query dynamically from yet unknown columns. We don’t know how many columns will be in our table.

    So for this we use data from information_schema columns table. We need to pass the table name, but also the database name, as we might have the same table name in separate databases.

    We need a query that returns col1,col2,col3,col4,col5 to us on the CONCAT_WS “string”

    So for this we run a query

    SELECT group_concat(column_name SEPARATOR ",") into @x from information_schema.columns where table_name=@tb and table_schema=@db;
    

    One more thing to mention. When we used the length() and replace() method to find out how many columns were concatenated, we have to make sure we do not have commas among the values. But also take note that we can have really long values in our database cells. For both of this trick we use method ASCII(‘value’), which will return the ASCII char of the first char, which cannot be comma and will return null for null columns.

    That being said we can compact all this in the above comprehensive solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if there is any way (in C) to get the contents of
I'm just wondering if there is a quick way to echo undefined variables without
I was just wondering if there is any difference between the two different new
Just wondering if there is an easy way to add the functionality to duplicate
Just wondering if there is a better way to write the following PL/SQL piece
I'm just wondering if there's a better way of doing this in SQL Server
I was just wondering if there is an elegant way to set the maximum
I was just wondering whether there is some way to do this: I have
I'm just wondering if there can be a case where the hostname can be
Just wondering if anyone knew off the top of their heads if there was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.