如何使用 SQL 选择给定工作日的条目?

编程入门 行业动态 更新时间:2024-10-23 20:31:09
本文介绍了如何使用 SQL 选择给定工作日的条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以使用此查询来选择日期为星期一的所有订单:

I could use this query to select all orders with a date on a monday:

SELECT * from orders WHERE strftime("%w", date)="1";

但据我所知,使用索引无法加快速度,因为必须计算每一行 strftime.

But as far as I know, this can't be speed up using an index, as for every row strftime has to be calculated.

我可以添加一个存储工作日的附加字段,但我想避免它.是否有使用索引的解决方案,或者我错了并且这个查询实际上工作正常?(这意味着它不必遍历每一行来计算结果.)

I could add an additional field with the weekday stored, but I want to avoid it. Is there a solution that makes use of an index or am I wrong and this query actually works fine? (That means it doesn't have to go through every row to calculate the result.)

推荐答案

如果您想要所有星期一,您需要进行现场扫描或顺序扫描.您可以做的是计算实际日期,例如一年内所有星期一的日期.条件 WHERE date IN ('2009-03-02', '2009-02-23', ...) 将使用索引

If you want all Mondays ever, you'd need a field or sequential scan. What you could do, is calculate actual dates for example for all Mondays within a year. The condition WHERE date IN ('2009-03-02', '2009-02-23', ...) would use index

更多推荐

如何使用 SQL 选择给定工作日的条目?

本文发布于:2023-10-14 21:55:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492324.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条目   工作日   如何使用   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!