如何从crontab获得小时并在MySQL中存储时按订单排序?

发布于 2025-01-27 15:55:24 字数 324 浏览 3 评论 0原文

我有一个装有crontab的菲尔德,现在我想将桌子符合符合crontab小时的桌子,如何编写SQL? 例如,crontab字段是:

7 9 9 */1 * ?, 
0 9 8 */1 * ?,
0 9 19 */1 * ?;

我希望结果是

0 9 8 */1 * ?, 
7 9 9 */1 * ?,
0 9 19 */1 * ?;

现在我只能像这样写SQL:

select * from tb_student order by crontab  desc

I have a feild that store crontab ,now I want to sort the tablee accordring to the crontab hour,how to write the sql?
For example,the crontab field is :

7 9 9 */1 * ?, 
0 9 8 */1 * ?,
0 9 19 */1 * ?;

I want the result is

0 9 8 */1 * ?, 
7 9 9 */1 * ?,
0 9 19 */1 * ?;

Now I can only write the sql like this:

select * from tb_student order by crontab  desc

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

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

发布评论

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

评论(1

吾家有女初长成 2025-02-03 15:55:24

因为seconds不可用于crontab表达式,并且可能有一些特殊字符,如

强制性字段名称允许的值特殊字符
no0-59* /, - < / code>
分钟0-59* /, - < / code>
小时0-23* /, - < / < / < / < /代码>
每月1-31* /, - L W < / code>
1-12或Jan-dec* /, - < / code>
每周一次0-6或太阳-sat* /, - l#< / code>
年度1970–2099年* /, - < / code>,

如果我们确保小时为数字且格式一样。

0 9 19 */1 * ?

我们可以尝试使用substring_index获得您的预期订单号。

SELECT *
FROM T
ORDER BY CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(Crontab, ' ', 3), ' ', -1) AS SIGNED)

sqlfiddle

IMHO, I would create a column to store a value which can create索引并改善性能,如果您想使用该订单或过滤条件,而不是 crontab列的订单。

Because Seconds is not Mandatory for crontab expression and there might be some Special characters as below tables.

Field nameMandatory?Allowed valuesSpecial characters
SecondsNo0-59* / , -
MinutesYes0-59* / , -
HoursYes0-23* / , -
Day of monthYes1-31* / , - L W
MonthYes1-12 or JAN-DEC* / , -
Day of weekYes0-6 or SUN-SAT* / , - L #
YearNo1970–2099* / , -

if we make sure the hour will be number and as same format.

0 9 19 */1 * ?

We can try to use SUBSTRING_INDEX to get part of your expect order number.

SELECT *
FROM T
ORDER BY CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(Crontab, ' ', 3), ' ', -1) AS SIGNED)

sqlfiddle

IMHO, I would create a column to store a value which can create an index and improve the performance if your want to use that be order or filter condition instead of order by crontab column.

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