2011-07-19 40 views
2

2つのテーブルがあります。最初のデータは、カテゴリに基づく最初のデータと2番目のメタデータです。データ内の列に基づく条件

table1 
id, category1, category2, custom1, custom2, custom3... 

table2 
category1, category2, denom, field 

例:表1にフィールド我々はメタデータを持っているtable2のポイントで

table1 
AAAAA, Car, Compact, Red, Automatic, 150bhp 

table2 
Car, Compact, Color, custom1 

'フィールド' 列。

今私がやってみたいのは、何とかsqlの 'field'列の値を列として使用することです。

select * from table1 
where table1.category1 = 'Car' 
    and table1.category2 = 'Compact' 
    and table1.category1 = table2.category1 
    and table1.category2 = table2.category2 
    and table2.denom = 'Color' 
    and table1.(value of table2.field) = 'Red' 

これは単一のSQL文で実行できますか?

答えて

2

「カスタム」列の数が事前にわかっている可能性があります。

あなたは

and case table2.field 
     when 'custom1' then table1.custom1 
     when 'custom2' then table1.custom2 
     when 'custom3' then table1.custom2 
     ... 
     else NULL 
     end 
     = 'Red' 
+0

であなたの答えをありがとう

and table1.(value of table2.field) = 'Red' 

を置き換えることができ、これはあなたがいる場合、クエリを編集する必要がありますよう列が表示されている必要はありませんでした何かを期待していました列は将来追加されます。しかし、これは動作するはずです:) – Mikael

関連する問題