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 8118115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:22:58+00:00 2026-06-06T04:22:58+00:00

Table ——————————————— | id | first_name | middle_name | last_name | ——————————————— | 1

  • 0

Table

---------------------------------------------
| id | first_name | middle_name | last_name |
---------------------------------------------
| 1  | abc        |             | def       |
---------------------------------------------
| 2  |            |             | xyz       |
---------------------------------------------
| 3  |  xyz       |             |           |
---------------------------------------------          

I want sql query which gives output as fields name which contains values and exclude fields which has empty value or null value.

like

id,
first_name,
last_name

  • 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-06-06T04:23:00+00:00Added an answer on June 6, 2026 at 4:23 am

    First try, see fiddle for result:

    http://sqlfiddle.com/#!2/b6d7b/7

    SELECT
      CONCAT_WS(',',
          IF(id          IS NULL, NULL, 'id'),
          IF(first_name  IS NULL, NULL, 'first_name'),
          IF(middle_name IS NULL, NULL, 'middle_name'),
          IF(last_name   IS NULL, NULL, 'last_name')
      )
    FROM person
    

    To my best knowledge you can’t create MySQL queries which uses dynamically created table/field names. The query optimizer has to exactly know before actual execution what tables and fields will be used in the query, that’s why the above query contains manually written field names.

    While you can create queries about database columns using INFORMATION_SCHEMA, you can’t use the received column names for querying, unless if you generate the needed sql code with php code, stored procedure, etc.

    See http://dev.mysql.com/doc/refman/5.0/en/columns-table.html

    If you wanted to find persons with ‘joh’ in their names, and the field names actually containing ‘joh’, you could use a similar query:

    http://sqlfiddle.com/#!2/f2597/3

    SELECT
      id AS person_id,
      CONCAT_WS(',',
          IF(first_name  LIKE '%joh%', 'first_name'  , NULL),
          IF(middle_name LIKE '%joh%', 'middle_name' , NULL),
          IF(last_name   LIKE '%joh%', 'last_name'   , NULL)
      ) AS name_parts
    FROM person
    WHERE first_name  LIKE '%joh%'
       OR middle_name LIKE '%joh%'
       OR last_name   LIKE '%joh%';
    

    If you want every column name, where there is at least one NOT NULL value in that column, then try this query:

    http://sqlfiddle.com/#!2/8dec7/5

    SELECT
      CONCAT_WS(',',
          IF(COUNT(id          )>0, 'id'         ,NULL),
          IF(COUNT(first_name  )>0, 'first_name' ,NULL),
          IF(COUNT(middle_name )>0, 'middle_name',NULL),
          IF(COUNT(last_name   )>0, 'last_name'  ,NULL)
      )
    FROM person;
    

    It uses COUNT( expr ), which ( http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_count ) Returns a count of the number of non-NULL values of expr.

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

Sidebar

Related Questions

table A amount user_id 100 abc 200 cdf 300 def table B Idno user_id
Table: ProductComapny Fields: id , name ProductComapny content 10000 recorde. now i want id
Table A has two columns: ID and name I want to input 3 ID
Table A has a number of rows, only some of which have (multiple) children
Table Schema Table Name: file_manager_folder Rows: id , parentId , name My query simulates
Table is huge. For testing application I want to delete all, except 1000 records
Table: laterecords ----------- studentid - varchar latetime - datetime reason - varchar My Query:
table data of 2 columns category and subcategory i want to get a collection
Table: UserId, Value, Date. I want to get the UserId, Value for the max(Date)
table 'sample' contaning fields [AffiliateId] as a primary key, [AffiliateCode] L, [Name] , [AddressId]

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.