如何访问mysql子查询中的外部列值?

时间:2013-08-21 20:29:02

标签: mysql select

在mysql中我有这个查询

SELECT m.*
FROM members m
RIGHT JOIN (SELECT 
                IF(`from member_id`=1, `to member_id`, `from member_id`) as other_id, text, `date sent`
             FROM message
             WHERE ((`from member_id`=1 AND `to member_id`=m.id) OR (`to member_id`=1 OR `from member_id`=m.id))
             ORDER BY `date sent` DESC
             LIMIT 1
            ) as t on 1=1
ORDER BY t.`date sent` DESC

我收到了这个错误:

Unknown column 'm.id' in 'where clause' 

如何在子查询select语句中传递成员列值?

我正在创建此子查询,因此它的计算结果为1行,然后我想将它附加到外部select语句的右侧。

感谢。

1 个答案:

答案 0 :(得分:0)

您需要在子查询中SELECT from member_id / to member_id个值。然后,您可以在派生表上加入表m,您可以在其中访问值。

) as t on t.`from member_id` = 1 AND t.`to member_id` = m.id
OR t.`to member_id` = 1 OR t.`from member_id` = m.id