2017-03-02 102 views
-1

当前我有一个cop_config表。我想要的是使用where条件获取一些(不是全部)列名。现在,我只能用下面的代码来获取所有的列名..如何从表中使用where条件获得列名称

$cop_value = "SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'cop_config' and is_nullable = 'NO'"; 
     $cop_result = $conn->query($cop_value); 
     while($cop_row = $cop_result->fetch_assoc()){ 
     $cop_row = implode(',', $cop_row); 
     echo $cop_row; 
    } 

currenlty我的输出是:

iddevice_keyfirst_pulse1first_pulse2first_pulse3first_pulse4first_pulse5first_pulse6first_pulse7first_pulse8second_pulse1second_pulse2second_pulse3second_pulse4second_pulse5second_pulse6second_pulse7second_pulse8 

这里是表: enter image description here

从表我想要的是根据device_key(在cop_config表中)只有那些有1个值的列名。我知道我的问题解释不太好,请看表格图片,以便更好地理解您的担忧。

像device_key YMR200,我只是想获得列名 - first_pulse1 & second_pulse2

与之相似的device_key VTA600我想获得列名 - first_pulse3 & second_pulse4。

------------------------------☺☻♀♥♣♦♣W ------ -------- ------------------------ 经过长时间的搜索和谷歌目前我使用

 $cop_value = "SELECT 'first_pulse1' from cop_config where first_pulse1 = 1 and device_key = 'VTA600' union 
select 'first_pulse2' from cop_config where first_pulse2 = 1 and device_key = 'VTA600' union 
select 'second_pulse1' from cop_config where second_pulse1 = 1 and device_key = 'VTA600' union 
select 'second_pulse2' from cop_config where second_pulse2 = 1 and device_key = 'VTA600'"; 


    $cop_result = $conn->query($cop_value); 
     while($cop_row = $cop_result->fetch_assoc()){ 

     echo $cop_row; 
    } 

我knwo我在这里作出错误的错误同时获取值...但我不知道如何从查询中获取值。 PLZ帮助 感谢..

+0

你搜索的PL/SQL基础的解决方案的输出?我能想到的是使用游标遍历行并获取其他列中每个device_key的状态。它应该工作,如果你有固定数量的列。 – Arun

+0

tnx您的回应。我会谷歌的PL/SQL,并让你知道如果这个工作 – metalhead101

回答

3

尝试
对于SQL Server:

SELECT 
    sys.columns.name AS ColumnName 
FROM 
    sys.columns 
WHERE 
    sys.columns.name = 'first_pulse1' 
    and 
    exists(select top 1 * from cop_config where device_key = 'YMR200') 

对于Oracle: see this

PLSQL: see this

对于MySQL: see this

+0

tncx你的回应。但我仍然收到意想不到的'最高'标识符 – metalhead101

+0

查看我贴过他们的标签后发布的其他链接? – ARr0w

+0

我已经经历了所有这些,但没有一个看起来很有希望,所以我已经转向联合方法。选择从cop_config 'first_pulse1',其中first_pulse1 = 1和device_key ='VTA600工会 从cop_config选择 'first_pulse2',其中first_pulse2 = 1和device_key ='VTA600工会 选择从cop_config 'second_pulse1',其中second_pulse1 = 1和device_key =' VTA600'union 从cop_config中选择'second_pulse2',其中second_pulse2 = 1和device_key ='VTA600' – metalhead101

0

最后经过长时间的搜索,我发现这个解决方案和它的工作。但我不确定这是否是正确的做法。目前我正在使用union来根据需要获取数据。这里是代码

$cop_value = "SELECT 'first_pulse1' from cop_config t where first_pulse1 = 1 and device_key = 'YMR200' union 
select 'first_pulse2' from cop_config t where first_pulse2 = 1 and device_key = 'YMR200' union 
select 'first_pulse3' from cop_config t where first_pulse3 = 1 and device_key = 'YMR200' union 
select 'first_pulse4' from cop_config t where first_pulse4 = 1 and device_key = 'YMR200' union 
select 'first_pulse5' from cop_config t where first_pulse5 = 1 and device_key = 'YMR200' union 
select 'first_pulse6' from cop_config t where first_pulse6 = 1 and device_key = 'YMR200' union 
select 'first_pulse7' from cop_config t where first_pulse7 = 1 and device_key = 'YMR200' union 
select 'first_pulse8' from cop_config t where first_pulse8 = 1 and device_key = 'YMR200' union 
select 'second_pulse1' from cop_config t where second_pulse1 = 1 and device_key = 'YMR200' union 
select 'second_pulse2' from cop_config t where second_pulse2 = 1 and device_key = 'YMR200' union 
select 'second_pulse3' from cop_config t where second_pulse3 = 1 and device_key = 'YMR200' union 
select 'second_pulse4' from cop_config t where second_pulse4 = 1 and device_key = 'YMR200' union 
select 'second_pulse5' from cop_config t where second_pulse5 = 1 and device_key = 'YMR200' union 
select 'second_pulse6' from cop_config t where second_pulse6 = 1 and device_key = 'YMR200' union 
select 'second_pulse7' from cop_config t where second_pulse7 = 1 and device_key = 'YMR200' union 
select 'second_pulse8' from cop_config t where second_pulse8 = 1 and device_key = 'YMR200'"; 


     $cop_result = $conn->query($cop_value); 
     while($cop_row = $cop_result->fetch_assoc()){ 
     $cop_row = implode(',', $cop_row); 
     echo $cop_row; 
    } 

,这是device_key YMR200

first_pulse1second_pulse2 
相关问题