I'm pretty new to SQL, so I'll try to keep it simple as to what I'm trying to do.
我对SQL很陌生,所以我会尽量简单地说明我要做的事情。
I have a system which I'm looking to select messages from, starting with the most recent first, select a maximum of 5 pieces of data, then after that resort them with the latest 'time' column last in order for them to display properly.
我有一个系统,我希望从最近的第一个开始选择消息,最多选择5个数据,然后在最后使用最新的“时间”列,以便他们正确显示。
Here's the syntax I'm using:
这是我正在使用的语法:
SELECT * FROM messages WHERE sender = '$uid' AND reciever = '$new_user_id'
OR reciever = '$uid' AND sender = '$new_user_id' ORDER BY id ASC
FROM (SELECT * FROM messages ORDER BY time DESC)
And here's the error that I'm getting:
这是我得到的错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (SELECT * FROM messages ORDER BY time DESC)' at line 1
您的SQL语法有错误;查看与MySQL服务器版本对应的手册,以便在第1行的“FROM(SELECT * FROM messages ORDER BY time DESC)”附近使用正确的语法
I understand that I'm getting something wrong here, but as SQL isn't really my thing, I haven't a clue where to turn.
我知道我在这里遇到了一些问题,但由于SQL不是我的事,我不知道该转向何处。
A little help would go to great causes, I've been banging my head on the wall for hours.
一点帮助就会有很大的原因,我一直在墙上撞了好几个小时。
Thanks.
3 个解决方案
#1
0
SELECT * FROM (
SELECT *
FROM messages
WHERE
(sender = '$uid' AND reciever = '$new_user_id')
OR (reciever = '$uid' AND sender = '$new_user_id')
ORDER BY time asc
LIMIT 5) AS a ORDER by a.time DESC
#2
2
Try this:
SELECT *
FROM (SELECT * FROM messages ORDER BY time DESC LIMIT 5)
WHERE ( sender = '$uid'
AND reciever = '$new_user_id' )
OR ( reciever = '$uid'
AND sender = '$new_user_id' )
ORDER BY time ASC;
EDIT Edited to resort with ascending time values in last 5 items inserted.
编辑在插入的最后5个项目中编辑了具有升序时间值的度假村。
#3
0
No need for the subquery it seems. Take off the subquery and tack on ORDER BY time DESC LIMIT 5.
看起来不需要子查询。取消子查询并在ORDER BY时间DESC LIMIT 5上进行操作。
#1
0
SELECT * FROM (
SELECT *
FROM messages
WHERE
(sender = '$uid' AND reciever = '$new_user_id')
OR (reciever = '$uid' AND sender = '$new_user_id')
ORDER BY time asc
LIMIT 5) AS a ORDER by a.time DESC
#2
2
Try this:
SELECT *
FROM (SELECT * FROM messages ORDER BY time DESC LIMIT 5)
WHERE ( sender = '$uid'
AND reciever = '$new_user_id' )
OR ( reciever = '$uid'
AND sender = '$new_user_id' )
ORDER BY time ASC;
EDIT Edited to resort with ascending time values in last 5 items inserted.
编辑在插入的最后5个项目中编辑了具有升序时间值的度假村。
#3
0
No need for the subquery it seems. Take off the subquery and tack on ORDER BY time DESC LIMIT 5.
看起来不需要子查询。取消子查询并在ORDER BY时间DESC LIMIT 5上进行操作。