提问者:小点点

如何根据其他表行值选择数据?


我有两个表:“工厂”和“商店”

Factory 
id (P.K.) 
specialCode 
...


Store
id (P.K.)
factory (F.K.)
...

我需要选择stores,哪个工厂有specialCode“9”(它是一个整数值)。 我怎么能这么做呢? 我需要类似select*from store where factory.specialcode=9的内容,我如何获取外部表行的值?


共2个答案

匿名用户

只需在引用的列连接

SELECT 
   s.*,f.* 
from store S 
INNER JOIN  Factory  f ON s.factory  = f.id  WHERE f.specialCode=9

匿名用户

您可以使用exists:

select s.*
from store s 
where exists (select 1 from factory f where f.id = s.factory and f.specialCode = '9')