我有这个mysql查询,我只想查询我的SELECT语句的最后3行,并且我想再次重新排序我的查询到ASC顺序,因为子查询在DESC中。
SELECT * FROM(
SELECT *
FROM post_replies
WHERE post_replies.post_hash=:posthash
ORDER BY post_replies.reply_id DESC
LIMIT 3)
ORDER BY reply_id ASC
这是出现的错误
CDbCommand无法执行SQL语句:SQLSTATE [42000]: 语法错误或访问冲突:1248每个派生表必须具有 它自己的别名。执行的SQL语句是:SELECT * FROM(SELECT * FROM post_replies WHERE post_replies.post_hash =:posthash ORDER BY post_replies.reply_id DESC LIMIT 3)ORDER BY reply_id ASC
我不明白如何在定义here
的情况下将mysql Alias放在这里如何让我的查询只获取select语句的最后3行并以ASC顺序生成结果?
答案 0 :(得分:6)
错误只是告诉您需要调用您的子选择。您将子别名后的别名放在子选择之后。这应该有效:
SELECT a.* FROM(
SELECT *
FROM post_replies
WHERE post_replies.post_hash=:posthash
ORDER BY post_replies.reply_id DESC
LIMIT 3) a
ORDER BY a.reply_id ASC
答案 1 :(得分:2)
我不是MySQL专家,但别名会在派生表之后,如下所示:
SELECT * FROM(
SELECT *
FROM post_replies
WHERE post_replies.post_hash=:posthash
ORDER BY post_replies.reply_id DESC
LIMIT 3) ---> AS MyDerivedTable <---
ORDER BY reply_id ASC