我有2个表,一个是公司列表,另一个是产品列表。 产品链接到'company_id'列中的公司
为了更好地理解,这就是我想要做的事情:
SELECT * FROM Products WHERE company_id = (Company.id where name = Google)
撰写此声明的正确方法是什么?
答案 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'