是否可以从下表中选择下一行?
Table foo
Values:
312
911
128 <-- prev
389 <-- current
124 <-- next
993
213
我不确定如何选择此原因数字不合适。
答案 0 :(得分:1)
可以使用MySQL HANDLER语法。文档说它只适用于MyISAM
引擎:
“第三个HANDLER ... READ语法从表中取一行 自然行顺序“
自然行顺序是行存储在MyISAM中的顺序 表数据文件。
假设下表:
CREATE TABLE `a` (
`a_col` int(10) unsigned NOT NULL DEFAULT '0',
`d_col` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`c_col` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
PRIMARY KEY (`a_col`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
包含以下数据:
mysql> select * from a;
+-------+---------------------+-----------+
| a_col | d_col | c_col |
+-------+---------------------+-----------+
| 0 | 2013-11-20 15:25:55 | nürnberg |
| 3 | 2013-11-11 00:00:00 | NULL |
| 4 | 2013-11-21 12:32:56 | adsf |
| 11 | 2013-11-21 12:33:04 | adsf |
| 17 | 2013-11-21 12:32:46 | adsf |
| 30 | 2013-11-21 12:33:11 | adsf |
| 861 | 2013-11-21 12:33:01 | adsf |
+-------+---------------------+-----------+
现在你可以打开一个处理程序:
mysql> handler a open;
Query OK, 0 rows affected (0.00 sec)
读取任意行:
mysql> handler a read `PRIMARY` = (4);
+-------+---------------------+-------+
| a_col | d_col | c_col |
+-------+---------------------+-------+
| 4 | 2013-11-21 12:32:56 | adsf |
+-------+---------------------+-------+
1 row in set (0.00 sec)
读取先前按自然顺序读取的行:
mysql> handler a read `PRIMARY` NEXT;
+-------+---------------------+-------+
| a_col | d_col | c_col |
+-------+---------------------+-------+
| 11 | 2013-11-21 12:33:04 | adsf |
+-------+---------------------+-------+
1 row in set (0.02 sec)
等等:
mysql> handler a read `PRIMARY` NEXT;
+-------+---------------------+-------+
| a_col | d_col | c_col |
+-------+---------------------+-------+
| 17 | 2013-11-21 12:32:46 | adsf |
+-------+---------------------+-------+
1 row in set (0.00 sec)
答案 1 :(得分:0)
如果您未指定订单,则数据库引擎根本不会对数据进行排序。因此,无论您在需要订购特定记录后尝试选择的是什么。