![](/img/trans.png)
[英]How can I get the oldest entry (month-year) using month and year table column (int) in mysql?
[英]How can I concat month column and year column to yearmonth column
我有一个包含列(月、年和年月)的 SQL 表
Month year
5 2020
6 2020
11 2020
我想以这种格式插入 yearmonth 列值
yearmonth
202005
202006
202011
我尝试了几个 Datetime 函数,但没有用。
我认为使用字符串函数更简单:
select t.*, concat(year, lpad(month, 2, '0')) as yearmonth
from mytable t
如果需要, lpad()
将“缺少的” 0
到月份部分,然后您可以将其与年份连接起来。
另一方面,如果你想要一个数字结果,算术是要走的路:
select t.*, year * 100 + month as yearmonth
from mytable t
如果你想要一个字符串,我可能会使用:
select cast(year * 100 + month as char)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.