2017-03-02 11 views
-1

現在、私はcop_configテーブルを持っています。私が望むのは、条件を使用していくつかの(すべてではない)列名をフェッチすることです。今、私は次のコードを使用して、すべての列名を取得することができています..私の出力がある条件を使用してテーブルから列名を取得する方法

$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

私が欲しいのは、値が1のdevice_key(cop_configテーブル)の列名だけを取得することです。私は自分の問題の説明があまり良くないことを知っていますあなたの懸念のためのより多くの理解tnxのためのテーブルの図を見てください。

device_key YMR200で、私は、列名を取得したいように - first_pulse3 & second_pulse4 - device_key VTA600ためfirst_pulse1 & second_pulse2

similaryを私は、列名を取得します。

------------------------------☺☻♀♥♣♦♣WÇ-------- ------------------------ 長い検索とGoogleは現在、私は

 $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です。私はgoogle PL/SQLとあなたの応答のためにこの作品 – metalhead101

答えて

3

は、SQL Serverの場合は
を試してみてください:Oracleの

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:MySQLの see this

see this

+0

tncxをお知らせします。しかし、私は予期しない "トップ"の識別子を取得しています – metalhead101

+0

私はタグの後に投稿した他のリンクを参照してください? – ARr0w

+0

私はそれらのすべてを通過しましたが、どれも非常に有望ではないようですので、私は組合方法に移りました。first_pulse1 = 1とdevice_key = 'VTA600'の組み合わせで、 'first_pulse1 = 1'、device_key = 'VTA600'を選択します。cop_configから 'first_pulse2'を選択し、first_pulse2 = 1かつdevice_key = 'VTA600' VTA600 'cop_configから' second_pulse2 'を選択します。second_pulse2 = 1、device_key =' VTA600 ' – metalhead101

0

最後に、長年のグーグル・グーグルの後、私はこの解決法とその働きを見つけました。しかし、私はそれが正しいのかどうかはわかりません。現在、私は必要に応じてデータを取得するために共用体を使用しています。ここでは、コード

$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 
関連する問題