2013-02-26 113 views
1

我想在下面运行脚本,但总是获得名称字段的NULL值。MySQL JOIN返回NULL字段

SELECT u.name AS _user_name, s.name AS _school_name 
FROM fwg_files AS f 
LEFT JOIN users AS u ON u.id = f.user_id 
LEFT JOIN user_profiles AS up ON up.user_id = u.id 
LEFT JOIN school AS s ON s.id = up.profile_value 

这个问题在我看来在JOIN ON学校表上,我试图选择s.id,它也返回NULL值。

表fwg_files

id | user_id 
240 | 414 
241 | 436 

表用户

id | name 
414 | Name 1 
436 | Name 2 

表user_profiles

user_id | profile_value 
414  | "6" 
436  | "14" 

表学校

id | name 

6 | School 1 
14 | School 2 

谢谢

+1

请从用户显示一个记录每表的UserProfiles – cja 2013-02-26 11:06:48

回答

0

不知道你的数据和架构,但如果up.profile_value就像是“123”,你可以试试这个:

SELECT u.name AS _user_name, s.name AS _school_name 
FROM fwg_files AS f 
LEFT JOIN users AS u ON u.id = f.user_id 
LEFT JOIN user_profiles AS up ON up.user_id = u.id 
LEFT JOIN school AS s ON CONCAT('"', s.id, '"') = up.profile_value