如何在MySQL中获取值不为空的列名 [英] How to get column name whose value is not null in mysql

查看:394
本文介绍了如何在MySQL中获取值不为空的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有一个条目的表.我必须获取其值不为null的那些列值.请建议我查询MySQL,以便我可以实施.我的桌子是:

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 :

在此表中, 3 列的值为Null.所以我不希望这些列,查询应该返回不为null的值.

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

我也可以获取列名吗?就像我想获取列的名称,即 min_p5 ,其值不为null.因此,我可以将列名称拆分为字符串,并在计算中使用 5 .请建议我回答.

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.

推荐答案

我认为这是您需要的:

假设您的表名是订单" [请相应更改]

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...

这篇关于如何在MySQL中获取值不为空的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆