我有兩個MySQL表:MySQL變更多個查詢到單個查詢
tblcoach:包含字段(coachid,coachschoolid,otherstuff)
tblschool:包含字段(schoolid,schooldivision)
我試圖獲得僅限於與教練處於同一學校的那些學校的學校名單。
這段代碼我有工作,但有沒有辦法產生一個單一的MySQL查詢來實現相同的結果?
SELECT @cs := coachschoolid FROM tblcoach c;
SELECT @sd := s.schooldivision FROM tblschool s WHERE s.schoolid = @cs;
SELECT s2.schoolid, s2.schoolname FROM tblschool s2 WHERE schooldivision = @sd
爲什麼不使用嵌套查詢?用查詢本身替換你的變量,它變成一個查詢。 –