Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Q (kdb): nested where query

What are the ways to in Q to use the results of a nested query in a where clause?

I'm looking for something similar to SQL statement.

select from food where type_id in (
    select type_id from types where type_name = "fruit"
)
like image 622
Leonid Avatar asked Jul 17 '11 18:07

Leonid


People also ask

What is query and nested query?

Using Nested Queries with SELECT. In SQL, a query is an operation that retrieves data from a table in a database and always includes a SELECT statement. A nested query is a complete query embedded within another operation.

WHERE is nested query in SQL?

A Subquery or Inner query or a Nested query is a query within another SQL query and embedded within the WHERE clause. A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.

What is nested query with example?

A nested query is a query that has another query embedded within it. The embedded query is called a subquery. A subquery typically appears within the WHERE clause of a query. It can sometimes appear in the FROM clause or HAVING clause.

What is subquery in WHERE clause?

A subquery in a WHERE clause can be used to qualify a column against a set of rows. For example, the following subquery returns the department numbers for departments on the third floor. The outer query retrieves the names of employees who work on the third floor. WHERE floor = 3);


1 Answers

while that's the direct answer to your question, the best way do this is probably foreign keys:

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)
like image 154
Aaron Davies Avatar answered Sep 25 '22 06:09

Aaron Davies