SQLite根据其他表从表中选择

时间:2016-02-03 21:22:09

标签: sql sqlite

我有2个表,一个是公司列表,另一个是产品列表。 产品链接到'company_id'列中的公司

为了更好地理解,这就是我想要做的事情:

SELECT * FROM Products WHERE company_id = (Company.id where name = Google)

撰写此声明的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

将其写为子选择

SELECT * FROM Products WHERE company_id =(SELECT id FROM companies WHERE name = 'Google' limit 1)

或使用in()运算符

进行子选择
SELECT * FROM Products WHERE company_id in (SELECT id FROM companies WHERE name = 'Google')

或将其写为联接

SELECT p.* 
FROM Products p
inner join companies c on p.company_id=c.id
WHERE c.name='Google'