Processing the First or Last n Records
mysql>
mysql> CREATE TABLE messages (
-> msgID int(20) NOT NULL auto_increment PRIMARY KEY,
-> forumID int(11),
-> rootID int(11),
-> parentID int(11),
-> userID int(11),
-> subject varchar(80),
-> msgText text,
-> ts timestamp
-> );
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> INSERT INTO messages VALUES (60,1002,60,NULL,375,'cgi','','2001-03-27 08:35:28'),
-> (62,3,62,NULL,162,'Message','','2001-03-29 16:07:29');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql>
mysql>
mysql>
mysql>
mysql> SELECT LEFT(subject, 20) AS subj, forumID, ts
-> FROM messages ORDER BY ts DESC LIMIT 5;
+---------+---------+---------------------+
| subj | forumID | ts |
+---------+---------+---------------------+
| Message | 3 | 2001-03-29 16:07:29 |
| cgi | 1002 | 2001-03-27 08:35:28 |
+---------+---------+---------------------+
2 rows in set (0.00 sec)
mysql>
mysql>
mysql> SELECT LEFT(subject, 20) AS subj, forumID, ts FROM messages
-> WHERE rootID=msgID AND forumID=1006
-> ORDER BY ts DESC LIMIT 5;
Empty set (0.00 sec)
mysql>
mysql> drop table messages;
Query OK, 0 rows affected (0.00 sec)
Related examples in the same category