这可能是我用MySQL无法理解的东西,但是在浪费了我的一天来解决StackOverflow的相关问题而没有解决问题之后,我决定询问它。
SELECT users.idUser, users.name, categoryName
FROM users
LEFT JOIN (
SELECT `translation` as categoryName
FROM localization,
usercategories
WHERE localization.`string` = usercategories.name
AND usercategories.idUserCategory = users.idUserCategory
)
as Something
WHERE users.idUser != 1
ORDER BY users.name ASC
无论我今天尝试哪个包含子查询的查询,我都会在几乎相同的地方得到相同的语法错误:紧跟在子查询的别名之后(在这种情况下,Something
)。
#1064 - 您的SQL语法出错;查看与MySQL服务器版本对应的手册,以便在第11行
'WHERE users.idUser != 1 ORDER BY users.name ASC LIMIT 0, 30'
附近使用正确的语法
答案 0 :(得分:1)
您可以重新组织查询,以便它不需要子查询。这样您还可以从任何表中向select中添加更多列。而且,它更正确。
SELECT
users.idUser,
users.name,
localization.`translation` as categoryName
FROM users
LEFT JOIN usercategories ON usercategories.idUserCategory = users.idUserCategory
LEFT JOIN localization ON localization.`string`= usercategories.name
WHERE users.idUser <> 1
ORDER BY users.name ASC
答案 1 :(得分:1)
这个问题是您错过了联接的ON子句。您需要选择一个条件将两个表连接在一起,如下所示:
SELECT stuff
FROM stuff
LEFT JOIN (other stuff)
ON stuff.something = otherstuff.something. // Add here.
答案 2 :(得分:1)
你的JOIN标准是非ANSI的,并且没有ON子句......也许是因为它导致它?试试这个,稍微优化一下:
SELECT Usr.idUser AS idUser
,Usr.name AS name
,UsrCat.translation AS categoryName
FROM users AS Usr
LEFT OUTER JOIN usercategories AS UsrCat
ON UsrCat.idUserCategory = Usr.idUserCategory
LEFT OUTER JOIN localization AS Lcl
ON Lcl.string = UsrCat.name
WHERE Usr.idUser <> 1
ORDER BY Usr.name ASC
不需要子查询,应该是非常高效的。