三表数据查询
三个表:
a表,字段UserID
b表,字段UserID和organizationID
c表,字段organizationID和organizationName
现想从userID知道该UserID相应的organizationName
该如何写存储?要效率高一些.
------解决方案--------------------SELECT c.organizationName
FROM a INNER JOIN b
ON a.UserID = b.UserID
INNER JOIN c
ON b.organizationID = c.organizationID
------解决方案--------------------实际上,只需对表b和表a进行内连接就可以查询了,因为要找出organizationName,就必须知道该organizationName对应的organizationID,而每个organizationID又对应一个UserID,所以,
select b.userid,c.organizationName
from b inner join c on b.organizationID=c.organizationID