selecting column names

Discussion in 'Databases' started by neilfurry, Dec 21, 2017.

  1. #1
    hi Mate,

    i need help on this,

    how can i select column names instead of its value, so it looks like this

    SELECT COLUMN_NAME WHERE row='002452' AND COLUMN_NAME IS NULL

    so it will display those NULL columns.

    row is the location of the id of an employee

    Hope to get help on this.

    Cheers!

    Neil
     
    neilfurry, Dec 21, 2017 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,807
    Likes Received:
    4,534
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Are you wanting to know which columns have no value?
     
    sarahk, Dec 22, 2017 IP
  3. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #3
    You can't select only column names because it would not make any sense. In other words, when you select something from column name, you need to enter the name of this column. So take a look at your query:

    SELECT COLUMN_NAME WHERE row='002452' AND COLUMN_NAME IS NULL

    This is what you do: you are trying to select the name of column when you already know the name. It's just not logical. Not to mention that you need to select by columns, not by rows; therefore, WHERE row='002452' doesn't make any sense (and will never work) too.

    Here's the basic tutorial that should be useful to you during your learning steps - http://www.mysqltutorial.org/mysql-select-statement-query-data.aspx
     
    phpmillion, Dec 22, 2017 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,807
    Likes Received:
    4,534
    Best Answers:
    123
    Trophy Points:
    665
    #4
    I guess you could do

    select *
    from `my_table`
    where `name` is null
    or `type` is null
    or `colour` is null


    and so on

    and then in your script you decide what to do

    You could do some of the sorting in the database, this will only show up the first problem field but you'll get the idea

    select *, case when `name` is null then 'name' else case when `type` is null then 'type' else case when `colour` is null then 'colour' end

    have a look at http://sqlfiddle.com/#!9/63e399/5 for a working example
     
    sarahk, Dec 22, 2017 IP