从mysql查询获取最后3行时出错

时间:2015-07-23 11:52:39

标签: php mysql pdo

我正在尝试从查询中获取最后3行,但我一直收到此错误:

SQLSTATE [42S21]:列已存在:1060重复列名'id'

拜托,我在这里做错了什么?

$sql = "SELECT * FROM (SELECT i.*, m.*, p.* FROM message i JOIN 
members m ON m.id = i.sender_id JOIN profile_photos p 
ON p.member_id = m.id   WHERE (receiver_id = :receive 
AND sender_id = :sent OR receiver_id = :sent AND sender_id = :receive) 
ORDER BY date DESC LIMIT 3) t ORDER BY date ASC";

1 个答案:

答案 0 :(得分:2)

您可能已在多个表中添加ID列,并选择声明SELECT i.*, m.*, p.*列出所有列。

选择您想要的特定列

SELECT i.id, i.col1, m.col2, p.col3 ...

OR,使用列别名,如

SELECT i.id as IID, m.*, p.id as PID