MySQL JOIN返回NULL字段

时间:2013-02-26 10:59:19

标签: mysql

我正在尝试运行下面的脚本,但总是为名称字段获取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学校表中我似乎遇到了这个问题,我尝试SELECT 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 个答案:

答案 0 :(得分: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