2010-10-05 42 views
1

繼續從MySQL multiple Id lookupsMYSQL在加入問題上比較

所以我正在尋找圍繞全文搜索限制視圖的其他方法。

我更新了提供的查詢......

SELECT t.teacherId, s1.subjectName AS name1, s2.subjectName AS name2, s3.subjectName AS name3 
FROM teacherProfile t 
LEFT JOIN 
     subjects s1 
ON  s1.subjectId = t.subjectOne 
LEFT JOIN 
     subjects s2 
ON  s2.subjectId = t.subjectTwo 
LEFT JOIN 
     subjects s3 
ON  s3.subjectId = t.subjectThree 

其中一期工程請客至少,但如果我加..

WHERE name1 = ENGLISH' 

到最後我得到的未知列名1,我可以甚至不對數據做簡單的查詢?

回答

1
SELECT t.teacherId, s1.subjectName AS name1, s2.subjectName AS name2, s3.subjectName AS name3 
FROM teacherProfile t 
LEFT JOIN 
     subjects s1 
ON  s1.subjectId = t.subjectOne 
LEFT JOIN 
     subjects s2 
ON  s2.subjectId = t.subjectTwo 
LEFT JOIN 
     subjects s3 
ON  s3.subjectId = t.subjectThree 
WHERE 'English' IN (s1.subjectName, s2.subjectName, s3.subjectName) 

,或者指的是你原來的問題,

SELECT t.teacherId, s1.subjectName AS name1, s2.subjectName AS name2, s3.subjectName AS name3 
FROM teacherProfile t 
LEFT JOIN 
     subjects s1 
ON  s1.subjectId = t.subjectOne 
LEFT JOIN 
     subjects s2 
ON  s2.subjectId = t.subjectTwo 
LEFT JOIN 
     subjects s3 
ON  s3.subjectId = t.subjectThree 
WHERE MATCH(s1.subjectName, s2.subjectName, s3.subjectName) AGAINST ('+English +Physics' IN BOOLEAN MODE) 

(僅適用如果兩個表是MyISAM

這將返回誰教兩個EnglishPhysics所有教師。

+0

魔術,感謝Quassnoi! – 2010-10-05 16:50:34

1

你不能引用列標籤;你必須引用該列,就像這樣:

WHERE s1.subjectName = 'ENGLISH' 
+0

很高興知道謝謝Brian。 – 2010-10-05 16:50:56

1

你得到未知的列名,因爲你有語法錯誤

改變它

s1.subjectName =「英語」