【问题标题】:get first and last element from sqlite database从 sqlite 数据库中获取第一个和最后一个元素
【发布时间】:2021-08-23 13:07:22
【问题描述】:

以下示例 -> https://www.tutorialspoint.com/how-to-get-the-first-and-last-record-of-the-table-in-mysql

要获取数据库的第一个和最后一个元素,我们可以使用 UNION。 我有一个名为 location 的表,其中包含三个查询。

我想获取 id 为 1 的行,在这种情况下为 3(db 会大得多) 所以我尝试了

(select * from location order by id ASC LIMIT 1)
UNION
(select * from location order by id DESC LIMIT 1);

但它在我的示例中不起作用。 sqlite 是否允许使用 UNION 操作?

【问题讨论】:

    标签: sql sqlite sql-order-by union sql-limit


    【解决方案1】:

    SQLite 支持UNION,但不喜欢括号内的查询。

    将每个查询用作子查询:

    select * from (select * from location order by id ASC LIMIT 1)
    UNION
    select * from (select * from location order by id DESC LIMIT 1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 2016-06-05
      • 2018-02-22
      • 2014-12-19
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      相关资源
      最近更新 更多