2011-07-17 15 views
12

where節で入れ子クエリの結果を使用する方法をQに教えてください。Q(kdb):入れ子クエリ

私はSQLと似たようなものをお探ししています。

select from food where type_id in (
    select type_id from types where type_name = "fruit" 
) 

答えて

8
select from food where type_id in (exec type_id from types where type_name like "fruit") 

あなたのクエリは、述語にに渡すと、文字列の平等のためのような関数を使用しているものから離れて、ほぼ正しかったです。リストを受け入れるときだけ、テーブルを渡しています。クエリをリストとして送信するには、execを使用します。

6

それはあなたの質問に直接答えですが、最良の方法はこれを行う、おそらく外部キーである:それを行うの

q)types:([type_id:`apple`orange`cucumber]type_name:`fruit`fruit`vegetable) 
q)food:([type_id:`types$`apple`orange`cucumber]price:3?2.) 
q)meta food 
c  | t f  a 
-------| --------- 
type_id| s types 
price | f   
q)select from food where type_id.type_name=`fruit 
type_id| price  
-------| --------- 
apple | 0.4593231 
orange | 1.383906 
q) 
4

別の方法:

select from food where type_id in (select type_id from types where type_name like "fruit")[`type_id] 
関連する問題