我正在尝试使用LIMIT
和OFFSET
从数据库中检索记录,但是当我在最后添加ORDER BY id
时,它会失败。
我的代码如下:(这个工作正常)
$sql6 = "SELECT * FROM products WHERE cat = 'category' LIMIT 10 OFFSET 0";
$result6 = $conn->query($sql6);
if ($result6->num_rows > 0) {
while($row6 = $result6->fetch_assoc()) {
echo $row6["position"];
} } else{}
添加ORDER后,它不会显示任何记录:
$sql6 = "SELECT * FROM products WHERE cat = 'category' LIMIT 10 OFFSET 0 ORDER BY id DESC";
$result6 = $conn->query($sql6);
if ($result6->num_rows > 0) {
while($row6 = $result6->fetch_assoc()) {
echo $row6["position"];
} } else{}
感谢任何帮助。
答案 0 :(得分:3)
LIMIT
必须在声明的末尾:
SELECT * FROM products WHERE cat = 'category' ORDER BY id DESC LIMIT 10 OFFSET 0
选择 [全部| DISTINCT | DISTINCTROW] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [,select_expr ...] [FROM table_references [WHERE where_condition] [GROUP BY {col_name | expr |位置} [ASC | DESC],... [与ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr |位置} [ASC | DESC],...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE' file_name' [CHARACTER SET charset_name] export_options | INTO DUMPFILE' file_name' | INTO var_name [,var_name]] [FOR UPDATE |锁定共享模式]]
BTW:执行声明后检查错误