MYSQL比较连接问题

时间:2010-10-05 16:39:35

标签: mysql left-join

继续MySQL multiple Id lookups

所以我正在研究关于视图的全文搜索限制的其他方法。

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

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'

到最后我得到Unknown列名1,我甚至不能对数据进行简单查询吗?

3 个答案:

答案 0 :(得分: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的教师。

答案 1 :(得分:1)

你不能引用列标签;你必须引用该列,如下所示:

WHERE s1.subjectName = 'ENGLISH'

答案 2 :(得分:1)

由于语法错误

,您的列名未知

将其更改为

s1.subjectName ='ENGLISH'