如何使用where where where

时间:2017-03-02 04:49:28

标签: php sql

目前我有一个cop_config表。我想要的是使用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;
    }  

当前我的输出是:

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的列名。我知道我的问题解释不太好请查看表格图片,以便更好地了解tnx,以备不时之需。

就像在device_key YMR200中一样,我只想得到列名 - first_pulse1& second_pulse2

类似于device_key VTA600我想得到列名 - first_pulse3& second_pulse4。

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

      $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;
    }

我知道我在获取值时犯了大错...但我不知道如何从查询中获取值。 Plz的帮助 谢谢..

2 个答案:

答案 0 :(得分: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')

for Oracle: see this

PLSQL: see this

for mySQL: see this

答案 1 :(得分:1)

最后经过长时间的谷歌搜索,我发现了这个解决方案及其工作原理。但我不确定这是正确的做法。目前我正在使用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