【发布时间】: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 并让你知道这是否可行