MySQL SELECT JOIN返回字段列表中的未知列

时间:2018-09-10 21:35:08

标签: mysql

此查询返回“字段列表中的未知位置。”。

dummy_schema.positions

dummy_schema.positions

SELECT JSON_OBJECT(
'id', userData.id,
'email', userData.email,
'first_name', userData.first_name,
'last_name', userData.last_name,
'mood', userData.mood,
'uuid', userData.uuid,
'role', userData.role,
'rawRole', IF(userData.role = 'cls_administrator', 'Administrator', IF(userData.role = 'cls_moderator', 'Moderator', IF(userData.role = 'cls_member', 'Member', ''))),
'position', userData.position,
'rawPosition', position.position,
'lastSeen', userData.lastSeen,
'added', userData.added) AS profile

FROM dummy_schema.users AS userData
LEFT JOIN dummy_schema.positions AS position ON userData.position = position.id
WHERE id = 1;
  

MySQL V6.3工作台

1 个答案:

答案 0 :(得分:1)

您可以尝试给dummy_schema.positions表使用其他别名而不是position,因为您的别名为position,但是有一个具有相同名称position的列将使数据库引擎混乱。

SELECT JSON_OBJECT
(
    'id', userData.id,
    'email', userData.email,
    'first_name', userData.first_name,
    'last_name', userData.last_name,
    'mood', userData.mood,
    'uuid', userData.uuid,
    'role', userData.role,
    'rawRole', IF(userData.role = 'cls_administrator', 'Administrator', IF(userData.role = 'cls_moderator', 'Moderator', IF(userData.role = 'cls_member', 'Member', ''))),
    'position', userData.position,
    'rawPosition', p.position,
    'lastSeen', userData.lastSeen,
    'added', userData.added
) AS profile  
FROM dummy_schema.users AS userData
LEFT JOIN dummy_schema.positions AS p ON userData.position = p.id
WHERE id = 1;