php - 如何跳过 SQL 查询中的一行?

标签 php mysql

我列出了我的数据库中的产品,我在其中选择了最新的产品作为主要功能。其余按最新产品排序。

所以我有两个查询,看起来像这样。

SELECT * FROM products ORDER BY product_id DESC LIMIT 1

SELECT * FROM products ORDER BY product_id DESC LIMIT 4

但是...这使得最新产品在我当前的布局中出现了两次。 (功能)。

如何让第二个查询跳过最新条目?

最佳答案

我们可以指定 LIMIT 子句改为从第二行开始,然后获取接下来的 4 行。

SELECT * FROM products ORDER BY product_id DESC LIMIT 1,4

Syntax是:

[LIMIT {[offset,] row_count | row_count OFFSET offset}]

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return.

The offset of the initial row is 0 (not 1):

关于php - 如何跳过 SQL 查询中的一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53544054/

相关文章:

mysql - sql 使用 count with group by 和 having

php - 通过$_POST处理textarea内容后保留多行

php从mysql查询中选择输入复选框

php - 如何使用 PHP PDO 检测不可用的数据库?

mysql - SQL 按 DATENAME 分组

mysql - 合并两列并按 asc 排序

mysql - 使用 MYSQL 中的 GROUP BY 计算状态列上的 id 计数以及特定行计数的总和

php - 带有 keyBy 的 Laravel 响应返回对象和数组

php - 从 .post 检索数据

php - 在 MySQL 中只选择一个子字符串然后返回它