2

I have a table which has a single entry. I have to get those column values whose values are not null. Please suggest me query for MySQL so I can implement this. My table is :

enter image description here

In this table 3 columns have Null values. So I don't want these columns, query should return values which in not null.

Can I get the column name also? Like I want to get name of the column i.e min_p5 whose value is not null. So I can break the column name into strings and use 5 in my calculation. Please suggest me answer.

2
  • Please use SELECT CASE in case check column value is NOT NULL then return column name otherwise return NULL Commented Feb 13, 2014 at 7:39
  • please provide me the query.. Commented Feb 13, 2014 at 8:13

5 Answers 5

2

I think this is what you need:

Assuming your table name to be "orders" [pls change it accordingly]

$q="show columns from orders";
$res=mysql_query($q) or die(mysql_error());
$arr_field=array();
while($row=mysql_fetch_object($res)){
    $field=$row->Field;
    $q1="select ".$field." from orders where ".$field."!=0"; //if string then '0'
    $res1=mysql_query($q1) or die(mysql_error());
    if(mysql_num_rows($res1)>0){
        $arr_field[]=$field;
    }
}
$q="select ";
foreach($arr_field as $field){
    $q.=$field.",";
}
$q=rtrim($q,",");
$q.=" from orders";
$res=mysql_query($q) or die(mysql_error());
while($row=mysql_fetch_object($res)){
    foreach($arr_field as $field){
        print($field."==".$row->$field."<br/>");
    }
}

Run this and I hope you will get an idea...

Sign up to request clarification or add additional context in comments.

Comments

2
SELECT * 
FROM table 
WHERE YourColumn IS NOT NULL;

Source: MySQL SELECT only not null values

Comments

2
select *  from table where column_name is not null

Comments

1

Try following query for solve your problem,

select * from table
where column_name IS NOT NULL

Comments

0

It seems like you want to return:

| POINT_ID | BUS_ID | MIN_P5 | MIN_P15 |
|----------|--------|--------|---------|
|     P101 |   B101 |   1000 |    3000 |

because you want to exclude columns that have a zero value. It is not too easy to do this in MySQL because you need to use prepared statements:

SELECT
  CONCAT(
    'SELECT CONCAT(\'SELECT \',
    CONCAT_WS(\',\',',
    GROUP_CONCAT(
    CONCAT(
      'CASE WHEN EXISTS(SELECT * FROM TABLENAME WHERE ',
      `column_name`,
      '!=\'0\') THEN \'',
      `column_name`,
      '\' END')
      ),
    '),\' FROM tablename\') FROM tablename INTO @final_sql'
  )
FROM   `information_schema`.`columns` 
WHERE  `table_schema`=DATABASE() 
       AND `table_name`='tablename'
INTO @sql;

PREPARE stmt FROM @sql;
EXECUTE stmt;
PREPARE finalstmt FROM @final_sql;
EXECUTE finalstmt;

Please see fiddle here. If your columns are numbers and not strings, maybe some minor fixes are needed. However, I would suggest you to try a different approach or to rethink about your table structure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.