11

在子句Q中使用嵌套查询的结果有哪些方法?where

我正在寻找类似于SQL声明的东西。

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

3 回答 3

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

除了传递给in谓词并使用 like 函数进行字符串相等之外,您的查询几乎是正确的。当它只接受一个列表时,您正在传递一个表。要将查询作为列表发送,我使用exec来完成这项工作。

于 2011-08-11T12:03:06.763 回答
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)
于 2012-03-23T03:11:52.797 回答
3

另一种方法:

select from food where type_id in (select type_id from types where type_name like "fruit")[`type_id]
于 2017-07-20T11:42:05.700 回答