未知表MySQL加入联合查询

时间:2013-09-18 18:52:58

标签: mysql sql

我试图将两个具有另一个表的外键的表(group_message_log)联合起来。但是发生了错误。

错误:未知表'group_message_log'

SELECT `message_id`,group_message_log.* FROM `messages_log` AS `log_1` JOIN `group_message_log` AS `gm` ON `message_id` = gm.id WHERE `student_id`='128' 
UNION ALL 
SELECT `message_id`,group_message_log.* FROM `message_phone_log` AS `log_2` JOIN `group_message_log` AS `gm` ON gm.id = `message_id` WHERE (`phone`='XXX' OR `phone`='XXX')

1 个答案:

答案 0 :(得分:1)

您正在使用包含别名group_message_log AS gm的表名 所以你应该使用全名gm的别名group_message_log insetad,如下所示

SELECT `message_id`,gm.* FROM `messages_log` AS `log_1` JOIN `group_message_log` AS `gm` ON `message_id` = gm.id WHERE `student_id`='128' 
UNION ALL 
SELECT `message_id`,gm.* FROM `message_phone_log` AS `log_2` JOIN `group_message_log` AS `gm` ON gm.id = `message_id` WHERE (`phone`='XXX' OR `phone`='XXX')