mysql 不显示列名_如何在MySQL中获取值不为空的列名

bd96500e110b49cbb3cd949968f18be7.png

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 :

GaKB9.jpg

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.

解决方案

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."
");

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值