2017-03-02 99 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 
相關問題