![](/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.