我有三张桌子A, B, C
。
我必须从A
rollNumber = 11
获取SName
和来自Bname
的{{1}} B
然后BCode = 10
我写了这个查询:
select C.SName,C.Bname, C.ID from C where SName = "one obtained from A table" and BName = "one obtained from B table"
答案 0 :(得分:-2)
SELECT C.SName, C.BName, C.ID
FROM C
INNER JOIN (SELECT * FROM A WHERE rollNumber='11') as t1
ON t1.SName = C.SName
INNER JOIN (SELECT * FROM B WHERE Bcode='10') as t2
ON t2.BName = C.Bname;
您可以执行的一项优化是在执行连接之前先过滤到较小的临时表。这将导致JOIN中的行数少得多。另一个优化是索引JOIN ... ON子句和WHERE子句中涉及的所有列。