原来我有一个数据表是这样的:
┌─────────────────┬────────────────┬─────────────────────────┐
│ month (integer) │ year (integer) │ academic year (varchar) │
├─────────────────┼────────────────┼─────────────────────────┤
│ 1 │ 2018 │ 2017-2018 │
│ 12 │ 2018 │ 2017-2018 │
│ 3 │ 2019 │ 2018-2019 │
│ 3 │ 2019 │ 2015-2016 │
│ 8 │ 2019 │ 2018-2019 │
└─────────────────┴────────────────┴─────────────────────────┘
请注意不同的数据类型。
我的目标是获得这样的行:月份和年份形成一个属于同一行中学年的日期。学年从九月(9日)开始,到八月(8日)结束。例如,第三排不应包含在结果中,因为12/2018不属于2017-2018,而是2018-2019。
我的 WHERE
子句应该类似于: CONVERT(varchar(10), year-1) + '-' + CONVERT(varchar(10), year) = academic_year
如果月份小于9,
以及 CONVERT(varchar(10), year) + '-' + CONVERT(varchar(10), year+1) = academic_year
否则。
当然这个代码没有意义。我需要知道:
如何在数据类型之间转换,
如何连接属性和常量。
有可能吗?你知道吗?
1条答案
按热度按时间aemubtdh1#
首先,转换
month
以及year
实际的date
. 有很多方法可以处理你的数据。使用concat
把month
以及year
转换成mysql认为date
是一个。这会变成
year
以及month
从月初开始的单一日期。然后分开
academic_year
分为两列。我们可以利用格式总是YYYY-YYYY
(您可以使用where academic_year not rlike '^[0-9]{4}-[0-9]{4}$'
)使用left
以及right
获取第一个和最后4个字符。把这些变成学年开始和结束的日期。
现在你可以和
created_at
,start_period
,和end_period
使用between
比较。把它们放在一起。。。
请注意,在您完成了将查询转换为正确查询的工作之后,查询变得多么容易
date
类型。与其针对您的特定需求进行特殊的解析,不如将其规范化为真实的解析date
类型并使用内置的mysql日期函数。如果可能的话,您应该将模式更改为直接存储为
date
类型。它不仅使事情变得更简单,而且可以更快地为日期列编制索引以进行快速搜索。即使你做不到,最好看看这样的数据应该如何存储。为了向后兼容,请保留旧列,如果必须的话,只需添加新列即可。
填充新列。
把索引放在上面。
一旦完成了你的查询,任何类似的事情,都变得非常简单和快速。
因为一个学年的信息会被多次复制,所以第二个重新设计是将学年信息移到自己的表中,并将其作为外键引用。