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

  • SEARCH
  • Home
  • 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 996009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:48:43+00:00 2026-05-16T06:48:43+00:00

Is there a way to get a column indicating the number of NULL fields

  • 0

Is there a way to get a column indicating the number of NULL fields in a row? This would be within a SELECT statement.

For Example:

Field1  Field2  Num_Null
-----------------------
NULL     "A"      1

UPDATE: I want this query so I can sort based on how many Affiliates sales there are of a given Book. So having 3 affiliates would be sorted higher than having 2, regardless of which ones. There are about seven affiliates in my database, and that’s subject to grow. So any query requiring that each Affiliate field be specified would probably be too long

The table:

Affiliates_Cache – Primary key is Affiliate_ISBN, has the prices of the book on various affiliates (NULL if its not available). Affiliates_Cache is the one where i want to count the number of NULLs

  • 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-16T06:48:45+00:00Added an answer on May 16, 2026 at 6:48 am

    I’m not sure if there are neater methods, but this should work:

    SELECT Field1, Field2, ISNULL(Field1) + ISNULL(Field2) Num_Null
    FROM   YourTable;
    

    Test case:

    CREATE TABLE YourTable (Field1 varchar(10), Field2 varchar(10));
    
    INSERT INTO YourTable VALUES (NULL, 'A');
    INSERT INTO YourTable VALUES ('B', 'C');
    INSERT INTO YourTable VALUES ('B', NULL);
    INSERT INTO YourTable VALUES (NULL, NULL);
    

    Result:

    +--------+--------+----------+
    | Field1 | Field2 | Num_Null |
    +--------+--------+----------+
    | NULL   | A      |        1 |
    | B      | C      |        0 |
    | B      | NULL   |        1 |
    | NULL   | NULL   |        2 |
    +--------+--------+----------+
    4 rows in set (0.00 sec)
    

    UPDATE: Further to the updated question:

    If you have columns in your table that look like affiliate_1, affiliate_2, etc, this is rarely a good idea as you would be mixing data with the metadata. In general, a recommended fix is to use another dependent table for the users-to-affiliates relationships, as in the following example:

    CREATE TABLE users (
       user_id int, 
       user_name varchar(100),
       PRIMARY KEY (user_id)
    ) ENGINE=INNODB;
    
    CREATE TABLE users_affiliates (
       user_id int, 
       affiliate_name varchar(100),
       PRIMARY KEY (user_id, affiliate_name),
       FOREIGN KEY (user_id) REFERENCES users (user_id)
    ) ENGINE=INNODB;
    

    Then sorting the users table by the number of affiliates will look something like this:

    SELECT    u.*, d_tb.num_aff
    FROM      users
    JOIN      (
                 SELECT   user_id, COUNT(*) num_aff
                 FROM     users_affiliates
                 GROUP BY user_id
              ) d_tb ON (d_tb.user_id = u.user_id)
    ORDER BY  d_tb.num_aff DESC;
    

    The advantages are plenty, but most importantly it makes queries such as the above easy to write, and flexible enough to work with any number of affiliates (an not limited by the number of columns you allocated).

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

Sidebar

Related Questions

Is there any way to get (row,column) coordinates of a table? Example: tbl1: tbl2:
Is there a way to get this XML from select * from ANY_TABLE query?
is there any way we can get the column count from an FMResultSet row?
Is there a way to get all possible values from a MySQL enum column?
Is there a way to get the columns that a SQL query would return
Is there any way to get the effect of running python -u from within
Is there a way to get Windows audio mixer levels using WMI? For example,
Is there a way to actually get the column name that was updated in
Is there a way to easily get the column types of a query result?
Is there a way to get the entire contents of a single column using

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.