是否有可能为给定的mysql查询引入if else?此查询产生4列。这可以在一个列中吗?
SELECT
IF(regression.execution_date >= CONCAT(YEAR(CURDATE() - 1), '-','07', '-','01') AND regression.execution_date <= CONCAT(YEAR(CURDATE()), '-', '09', '-', '30'), CONCAT(YEAR(CURDATE() - 1), '.', '1'), 0) AS 'Q1',
IF(regression.execution_date >= CONCAT(YEAR(CURDATE() - 1),'-','10','-','01') AND regression.execution_date <= CONCAT(YEAR(CURDATE()), '-', '12', '-', '31'), CONCAT(YEAR(CURDATE() - 1), '.', '2'),0) AS 'Q2',
IF(regression.execution_date >= CONCAT(YEAR(CURDATE()), '-', '01', '-', '01') AND regression.execution_date <= CONCAT(YEAR(CURDATE()), '-', '03', '-', '31'), CONCAT(YEAR(CURDATE()), '.', '3'), 0) AS 'Q3',
IF(regression.execution_date >= CONCAT(YEAR(CURDATE()), '-', '04', '-', '01') AND regression.execution_date <= CONCAT(YEAR(CURDATE()), '-', '06', '-', '31'), CONCAT(YEAR(CURDATE()), '.', '4'), 0) AS 'Q4'
FROM
regression
解决方案:
感谢大家的评论。我用case实现了它。它有用。
SELECT
CASE
WHEN regression.execution_date >= CONCAT(YEAR(CURDATE() - 1), '-','07', '-','01') AND regression.execution_date <= CONCAT(YEAR(CURDATE()), '-', '09', '-', '30') THEN (CONCAT(YEAR(CURDATE() - 1), '.', '1'))
WHEN regression.execution_date >= CONCAT(YEAR(CURDATE() - 1), '-','10', '-','01') AND regression.execution_date <= CONCAT(YEAR(CURDATE()), '-', '12', '-', '31') THEN (CONCAT(YEAR(CURDATE() - 1), '.', '2'))
WHEN regression.execution_date >= CONCAT(YEAR(CURDATE()), '-','01', '-','01') AND regression.execution_date <= CONCAT(YEAR(CURDATE()), '-', '03', '-', '31') THEN (CONCAT(YEAR(CURDATE()), '.', '3'))
WHEN regression.execution_date >= CONCAT(YEAR(CURDATE()), '-','04', '-','01') AND regression.execution_date <= CONCAT(YEAR(CURDATE()), '-', '06', '-', '31') THEN (CONCAT(YEAR(CURDATE()), '.', '4'))
END as 'Quarter'
FROM
regression
答案 0 :(得分:0)
你想计算实际的季度吗?
使用它:
major
<强>样品强>
SELECT QUARTER(now());
答案 1 :(得分:0)
只需将您的日期改为7个月即可获得您想要的自定义区域:
SELECT(QUARTER(DATE_SUB(execution_date, INTERVAL 7 MONTH)))
FROM regression
如果您想使用原始方法,请继续阅读。我在查询中看到了几个问题/效率低下,我试图纠正。首先,因为您的查询位于regression
表上,所以在选择列时不需要显式引用此表,因此我不再使用它。其次,您试图将execution_date
(可能是日期类型)与字符串进行比较,字符串是CONCAT()
输出的类型。所以我使用STR_TO_DATE()
将手工建立的日期字符串转换为实际的日期类型。最后,我用一个IF
表达式替换了一组噩梦般的嵌套CASE
语句。
CASE WHEN execution_date >= STR_TO_DATE(CONCAT(YEAR(CURDATE() - 1), '-', '07', '-', '01'), '%Y-%m-%d') AND
execution_date <= STR_TO_DATE(CONCAT(YEAR(CURDATE()), '-', '09', '-', '30'), '%Y-%m-%d')
THEN CONCAT(YEAR(CURDATE() - 1), '.', '1')
WHEN execution_date >= STR_TO_DATE(CONCAT(YEAR(CURDATE() - 1), '-', '10', '-', '01'), '%Y-%m-%d') AND
execution_date <= STR_TO_DATE(CONCAT(YEAR(CURDATE()), '-', '12', '-', '31'),'%Y-%m-%d')
THEN CONCAT(YEAR(CURDATE() - 1), '.', '2')
WHEN execution_date >= STR_TO_DATE(CONCAT(YEAR(CURDATE()), '-', '01', '-', '01'), '%Y-%m-%d') AND
execution_date <= STR_TO_DATE(CONCAT(YEAR(CURDATE()), '-', '03', '-', '31'), '%Y-%m-%d')
THEN CONCAT(YEAR(CURDATE()), '.', '3')
WHEN execution_date >= STR_TO_DATE(CONCAT(YEAR(CURDATE()), '-', '04', '-', '01'), '%Y-%m-%d') AND
execution_date <= STR_TO_DATE(CONCAT(YEAR(CURDATE()), '-', '06', '-', '31'), '%Y-%m-%d')
THEN CONCAT(YEAR(CURDATE()), '.', '4')
ELSE '0' END AS theQuarter
FROM regression