Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL—SELECT中计算列的访问值_Mysql_Sql - Fatal编程技术网

MySQL—SELECT中计算列的访问值

MySQL—SELECT中计算列的访问值,mysql,sql,Mysql,Sql,我有一个查询,它使用LEFT JOIN和GROUP\u CONCAT生成一些计算列: SET @this_year = YEAR(CURDATE()); SET @next_year = YEAR(CURDATE()) +1; SET @last_year = YEAR(CURDATE()) -1; SELECT m.id, m.name, GROUP_CONCAT(`se1`.`name` ORDER BY `se1`.`id` ASC SEPARATOR ', ') AS

我有一个查询,它使用
LEFT JOIN
GROUP\u CONCAT
生成一些计算列:

SET @this_year = YEAR(CURDATE());
SET @next_year = YEAR(CURDATE()) +1;
SET @last_year = YEAR(CURDATE()) -1;

SELECT
    m.id, m.name,

    GROUP_CONCAT(`se1`.`name` ORDER BY `se1`.`id` ASC SEPARATOR ', ') AS this_year_events,
    GROUP_CONCAT(`se2`.`name` ORDER BY `se2`.`id` ASC SEPARATOR ', ') AS next_year_months,
    GROUP_CONCAT(`se3`.`name` ORDER BY `se3`.`id` ASC SEPARATOR ', ') AS last_year_months

FROM month m

LEFT JOIN schedule_event se1 ON
    (m.start_date BETWEEN se1.start_date AND se1.end_date) OR
    (m.end_date BETWEEN se1.start_date AND se1.end_date)
    
LEFT JOIN schedule_event se2 ON
    (m.start_date BETWEEN se2.start_date AND se2.end_date) OR
    (m.end_date BETWEEN se2.start_date AND se2.end_date)
    
LEFT JOIN schedule_event se3 ON
    (m.start_date BETWEEN se3.start_date AND se3.end_date) OR
    (m.end_date BETWEEN se3.start_date AND se3.end_date)

GROUP BY `m`.`id`
结果如下所示:

id    name             this_year_events      next_year_events      last_year_events            
====================================================================================
1     January          Training, Planning    (NULL)                (NULL)               
2     February         (NULL)                (NULL)                Audit, Budget    
3     March            Team Meeting          Team Away Day         (NULL)
现在我想在此查询中添加另一列:
active\u year
。此列将根据以下条件填充相应变量的值:

DEFAULT:
    active_year = @this_year

IF this_year_events IS NULL:
    active_year = @next_year

IF next_year_events IS NULL:
    active_year = @last_year
(如果满足后续条件,
active\u year
的值将覆盖上一个值)


我曾尝试为3个计算列分配一个变量,但它总是输出
NULL
。我就是想不通。

你只是想要
案例

with t as (
      <your query here>
     )
select t.*,
       (case when this_year_events is not null then @this_year
             when next_year_events is not null then @next_year
             else @last_year
        end) as active_year
from t;

谢谢戈登,太好了。我只是想知道做
COUNT(se1.name)!=0
,而不是
MAX(se1.name)不为NULL
?@GSTAR。这些是等价的。
select . . .,
       (case when max(se1.name) is not null then @this_year
             when max(se2.name) is not null then @next_year
             else @last_year
        end)
from . . .