我正在尝试运行下面的脚本,但总是为名称字段获取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值。当我将最后一行更改为RIGHT JOIN时,它开始工作,但我只能看到s.name值,其他值为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
谢谢
答案 0 :(得分:2)
如果必须使用profile_value
列上的引文,则需要使用CONVERT
和SUBSTRING_INDEX
的混合来删除引号,以便能够执行操作。
检查以下代码:
SELECT users.name as User, s.name as School
FROM fwg_files AS ff
LEFT JOIN users ON users.id = ff.user_id
LEFT JOIN user_profiles AS up ON up.user_id = users.id
LEFT JOIN school AS s ON s.id = CONVERT(SUBSTRING_INDEX(up.profile_value,'"',-2),UNSIGNED INTEGER);
此处working SQL Fiddle code带引号。