如何也使用 where 条件从表中获取列名

Posted

技术标签:

【中文标题】如何也使用 where 条件从表中获取列名【英文标题】:How to get the column name from the table using where condition too 【发布时间】:2017-03-02 04:49:27 【问题描述】:

目前我有一个 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

这是表格:

从表中我想要的是根据设备键(在 cop_config 表中)仅获取那些值为 1 的列名。我知道我的问题解释不太好,请查看表格图片以更好地理解 tnx 以解决您的问题。

就像在 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;
    

我知道我在获取值时在这里犯了错误……但我不知道如何从查询中获取值。请帮忙 谢谢..

【问题讨论】:

您是否搜索过基于 PL/SQL 的解决方案?我能想到的是使用游标遍历行并获取其他列中每个 device_key 的状态。如果您有固定数量的列,它应该可以工作。 tnx 供您回复。我会谷歌 PL/SQL 并让你知道这是否可行 【参考方案1】:

试试 对于 sql 服务器:

 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')

对于甲骨文: see this

PLSQL: see this

对于 mysql: see this

【讨论】:

tncx 供您回复。但我仍然得到了意想不到的“***”标识符 查看我在他们的标签之后发布的其他链接? 我已经完成了所有这些,但似乎没有一个很有希望,所以我已经转向联合方法..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' 在 mysql 工作台中它正在工作,因为它应该在我的实时 php 文件中它没有运行。我缺乏如何从查询中获取值。如果您有任何想法,请帮助 我已经更新了我的问题,如果您有解决方案帮助,请检查一次...【参考方案2】:

终于经过长时间的谷歌搜索,我找到了这个解决方案并且它的工作原理。但我不确定这样做是否正确。目前我正在使用 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

【讨论】:

以上是关于如何也使用 where 条件从表中获取列名的主要内容,如果未能解决你的问题,请参考以下文章

sql 如何把查询得到的结果如何放入一个新表中

如何从laravel中的表中选择所有列名?

从表中检索列名 -Redshift

如何使用 Go 在大查询中获取表列名列表

数据库表中操作语句

php pdo:获取表的列名