使用 DISTINCT 时 SQLite 结果乱序

发布于 2024-11-02 17:16:49 字数 519 浏览 0 评论 0原文

我有一个 SQLite 查询,其中按日期对结果进行排序。这很好用。但是,当我在 SQL 语句中引入“DISTINCT”时,结果不再保持顺序。

有什么想法吗?

SELECT date 
FROM details 
ORDER BY date(date); // Results are ordered correctly

     April 04, 2011
     April 04, 2011
     April 04, 2011
     April 03, 2011
     April 01, 2011
     March 25, 2011

SELECT DISTINCT date 
FROM details 
ORDER BY date(date); // Results are out of order

     April 01, 2011
     April 03, 2011
     April 04, 2011
     March 25, 2011

I have an SQLite query in which I am ordering the results by date. This works fine. However, when I introduce "DISTINCT" into the SQL statement, the results no longer stay in order.

Any thoughts?

SELECT date 
FROM details 
ORDER BY date(date); // Results are ordered correctly

     April 04, 2011
     April 04, 2011
     April 04, 2011
     April 03, 2011
     April 01, 2011
     March 25, 2011

SELECT DISTINCT date 
FROM details 
ORDER BY date(date); // Results are out of order

     April 01, 2011
     April 03, 2011
     April 04, 2011
     March 25, 2011

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咿呀咿呀哟 2024-11-09 17:16:49

date() 函数无法识别您的 date 列,因此 date(date) 给出 NULL

使用 ORDER BY NULL 尝试这两个查询,您将获得与 ORDER BY date(date) 相同的结果,

如果没有 DISTINCT,它将通过插入对它们进行排序命令。

对于DISTINCT,顺序来自DISTINCT本身,并应用于date列,该列是文本,因此按字母顺序排列。

SQLite 不支持月份名称。将数据更改为支持的格式(请参阅日期和时间函数)或将日期列转换为可识别的格式,然后将其提供给 date()(使用字符串函数)

例如(将 date 转换为格式 YYYY-mmm-dd< /code> 调用之前date() 有效):

(此处 CASE WHEN 仅限于您的数据,您可以将其扩展到所有月份)

SELECT DISTINCT date 
  FROM (SELECT *,
               substr(date, -4) year,
               substr(date, -8, 2) day,
               trim(substr(date, 1, length(date)-8)) month
         FROM details) as details
 ORDER BY date(year
               || '-'
               || CASE WHEN month = 'April' THEN '04' ELSE '03' END
               || '-'
               || day)

,将给出正确的带或不带 DISTINCT 的结果。

Your date column is not in recognized by the date() function, so date(date) gives NULL.

Try both queries with ORDER BY NULL you'll have the same results as with ORDER BY date(date)

Without DISTINCT it's ordering them by insert order.

With DISTINCT, the order comes from DISTINCTitself and is applied to the date column which is a text so it's in alphabetic order.

SQLite does not support month names. Change your data to supported formats (see Date And Time Functions) or transform the date column to recognized format before giving it to date()(with string functions)

For example (transforming date to the format YYYY-mmm-dd before calling date() works):

(here CASE WHEN is limited to your data, you can extend it to all months)

SELECT DISTINCT date 
  FROM (SELECT *,
               substr(date, -4) year,
               substr(date, -8, 2) day,
               trim(substr(date, 1, length(date)-8)) month
         FROM details) as details
 ORDER BY date(year
               || '-'
               || CASE WHEN month = 'April' THEN '04' ELSE '03' END
               || '-'
               || day)

, will give the correct results with or without DISTINCT.

甜`诱少女 2024-11-09 17:16:49

我认为您的日期字符串不是有效的日期格式。

尝试运行此 SQL 看看您会得到什么:

SELECT strftime('%Y-%m-%d', date) FROM details;

如果出现错误,则说明它没有正确解析您的日期。

您的第二个查询按字母顺序返回,而不是按时间顺序返回 - 如果它不是该字段中的有效日期,这就是原因。

I don't think your date string is in a valid date format.

Try running this SQL to see what you get:

SELECT strftime('%Y-%m-%d', date) FROM details;

If you get errors, it is not parsing your dates correctly.

Your second query is returning in alphabetic order, not chronological order - if it is not a valid date in that field, that would be the reason.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文