MySQL在where子句中给出了未知的列错误

时间:2013-01-18 14:14:37

标签: mysql sql

这是我的SQL语句,我无法运行它:

SELECT fm . * , mp.first_name, mp.last_name, mp.image, mp.member_id
FROM featured_members AS fm
JOIN member_profiles AS mp WHERE fm.member_id = mp.member_id
AND fm.member_type_id =6
AND fm.site_id =1

当我运行它时,它会给我#1054 - Unknown column 'fm.site_id' in 'on clause'错误

是否存在语法问题,因为fm表确实具有该site_id列但我不知道为什么SQL不提取它?

featured_members的我的表结构是这样的:

1 sr_no     
2 member_id     
3 member_type_id 
4 site_id   

member_profiles结构是:

1 member_profile_id 
2 member_id     
3 first_name    
4 middle_name   
5 last_name     
6 gender
7 image

5 个答案:

答案 0 :(得分:2)

你做错了: 使用JOIN时,必须使用ON子句,除非它是交叉连接。

SELECT fm . * , mp.first_name, mp.last_name, mp.image, mp.member_id
FROM   featured_members AS fm
JOIN   member_profiles  AS mp ON ( fm.member_id = mp.member_id )
WHERE fm.member_type_id =6
AND fm.site_id =1

答案 1 :(得分:1)

测试一下:

SELECT fm.*, mp.first_name, mp.last_name, mp.image, mp.member_id
FROM featured_members AS fm
LEFT JOIN member_profiles AS mp ON fm.member_id = mp.member_id
WHERE fm.member_type_id =6 AND fm.site_id =1

答案 2 :(得分:1)

查询中缺少“on”子句,MySQL需要它。因此,在我看来,Moddom提供的解决方案是正确的答案。

请参阅http://dev.mysql.com/doc/refman/5.0/en/join.html以及http://bugs.mysql.com/bug.php?id=13551

答案 3 :(得分:1)

试试这个......

SELECT fm . * , mp.first_name, mp.last_name, mp.image, mp.member_id
FROM featured_members AS fm, member_profiles AS mp
WHERE fm.member_id = mp.member_id
AND fm.member_type_id =6
AND fm.site_id =1

语句中的JOIN子句期望相应的ON,这是导致错误的原因

答案 4 :(得分:0)

试试这个: -

SELECT fm . * , mp.first_name, mp.last_name, mp.image, mp.member_id <br>
FROM featured_members fm , member_profiles mp <br>
where fm.member_id = mp.member_id <br>
AND fm.member_type_id =6 <br>
AND fm.site_id =1