gpt4 book ai didi

mysql - 连接年和周函数

转载 作者:行者123 更新时间:2023-11-29 11:36:53 26 4
gpt4 key购买 nike

mysql> select concat(yearval,weekval) as val1,concat(year(curdate()),week(curdate())) as val2 from wbr_weekly;
+-------+--------+
| val1 | val2 |
+-------+--------+
| 20161 | 201614 |
| 20162 | 201614 |
| 20163 | 201614 |
| 20164 | 201614 |
| 20165 | 201614 |
| 20161 | 201614 |
| 20162 | 201614 |
| 20163 | 201614 |
| 20164 | 201614 |
| 20165 | 201614 |
| 20161 | 201614 |
| 20162 | 201614 |
| 20163 | 201614 |
| 20164 | 201614 |
| 20165 | 201614 |
+-------+--------+
15 rows in set (0.00 sec)

mysql> select concat(yearval,weekval) as val1 from wbr_weekly where concat(yearval,weekval) < concat(year(curdate()),week(curdate()));
+-------+
| val1 |
+-------+
| 20161 |
| 20161 |
| 20161 |
+-------+
3 rows in set (0.00 sec)

我的目标是返回少于当前周的所有周。我认为第二个查询会做我想要的事情。但事实并非如此。有人可以解释一下我的查询出了什么问题吗?

最佳答案

在字符串比较中,“20161”、“20162”等在字典顺序上始终小于“201614”。您需要用前导零填充周数才能进行比较:“201601”、“201602”等:

select concat(yearval,weekval) as val1 from wbr_weekly 
where concat(yearval, right(concat('0', weekval), 2))
< concat(year(curdate()),week(curdate()));

关于mysql - 连接年和周函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36431304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com