'on子句'中的未知列'user.id'

时间:2017-04-23 10:58:02

标签: mysql

MySQL查询:

SELECT 
    *, 
    `message`.`status` as `message_status`, 
    `message`.`id` as `message_id`, 
    group_concat(response.response) as response, 
    group_concat(response.id) as response_id, 
    `message`.`user_id` as `sender` 
FROM `message` 
LEFT JOIN `pet_info` ON `pet_info`.`id` = `message`.`pet_id` 
LEFT JOIN `response` ON `response`.`message_id` = `message`.`id` 
LEFT JOIN `user` as sender ON `user`.`id` = `message`.`user_id` 
LEFT JOIN `user` as receiver ON `user`.`id` = `message`.`owner_id` 
WHERE `user_id` = '6' 
AND `pet_id` = '6' 
GROUP BY `message`.`id`

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

你正在对表格进行别名处理:

LEFT JOIN `user` as sender

这意味着,就查询的其余部分而言,没有user表。有一个sender表。所以你需要在ON子句中使用别名:

LEFT JOIN `user` as sender ON `sender`.`id` = `message`.`user_id`