2013-01-18 106 views
2

這是我的SQL語句,我無法運行未知列錯誤:MySQL允許在where子句

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 
+2

做什麼你的桌子看起來像? – DonSeba

+2

fm.site_id你有在featured_members表中的site_id列嗎?檢查它... – xurca

+0

我更新了它與表結構 – Seeker

回答

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

爲什麼這會解決'fm.site_id'的不存在? – eggyal

+0

所以與我們分享表列 – DonSeba

+1

我做了@DonSeba請現在檢查 – Seeker

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 
+1

爲什麼這會解決'fm.site_id'的不存在? – eggyal

+1

仍然不能正常工作 – Seeker

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子句期待相應的其上是什麼原因造成的錯誤

+0

感謝您的幫助:) – Seeker

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