如何在子查询中使用选择列别名?

时间:2013-08-13 13:40:33

标签: mysql sql

考虑此查询:

SELECT DATE_FORMAT(t.date, "%Y") training_year, 
COUNT(i.id) nb_trainees, 
nb_canceled_trainings, 
nb_done_trainings, 
(nb_canceled_trainings / (nb_canceled_trainings + nb_done_trainings)) cancellation_rate
FROM (
    SELECT COUNT(`trainings_trainings`.`id`) nb_canceled_trainings
    FROM  `trainings_trainings`
    WHERE `trainings_trainings`.`id_status` = 5
    AND DATE_FORMAT(`trainings_trainings`.`date`, "%Y") = training_year
) nct, (
    SELECT COUNT(`trainings_trainings`.`id`) nb_done_trainings
    FROM  `trainings_trainings`
    WHERE `trainings_trainings`.`id_status` IN (6, 7)
    AND DATE_FORMAT(`trainings_trainings`.`date`, "%Y") = training_year
) ndt,
`trainings_inscriptions` i
INNER JOIN `trainings_trainings` t ON i.`id_training` = t.id
GROUP BY training_year;

问题是我在子查询中无法访问training_year。我怎么能这样做?

我的第一个尝试是移动SELECT部分​​中的子查询,但在这样做之后,我不能将这些子查询的别名用于cancellation_rate计算。

修改

我的结果是@ Jafar的查询和一些修改:

SELECT current_year, nb_trainees, nb_canceled_trainings, nb_done_trainings, 
        (nb_canceled_trainings / (nb_canceled_trainings + nb_done_trainings)) cancellation_rate,
        (nb_trainees / nb_done_trainings) filling
FROM (
    SELECT COUNT(CASE WHEN `t`.`id_status` = 5 THEN 1 END) nb_canceled_trainings,
    COUNT(CASE WHEN `t`.`id_status` IN (6, 7) THEN 1 END) nb_done_trainings, (
        SELECT COUNT(`ti`.`id`) nb_trainees
        FROM `trainings_inscriptions` `ti`
        INNER JOIN `trainings_trainings` `tt` ON `ti`.`id_training` = `tt`.`id`
        WHERE DATE_FORMAT (`tt`.`date`, "%Y") = DATE_FORMAT (`t`.`date`, "%Y")
    ) nb_trainees, DATE_FORMAT (`t`.`date`, "%Y") current_year
    FROM `trainings_trainings` `t`
    GROUP BY DATE_FORMAT (`t`.`date`, "%Y")
) dummy;

这就是我得到的:

+------+----------+----------+------+--------+---------+
| year | trainees | canceled | done | ratio  | filling |
+------+----------+----------+------+--------+---------+
| 2012 |        0 |        0 |    0 | NULL   | NULL    |
| 2013 |       58 |        0 |    2 | 0.0000 | 29.0000 |
| 2014 |        2 |        1 |    0 | 1.0000 | NULL    |
+------+----------+----------+------+--------+---------+

2 个答案:

答案 0 :(得分:0)

尝试像这样修改子查询

SELECT COUNT(`trainings_trainings`.`id`) nb_canceled_trainings,
  DATE_FORMAT(`trainings_trainings`.`date`, "%Y") training_year
FROM  `trainings_trainings`
WHERE `trainings_trainings`.`id_status` = 5
GROUP BY training_year

然后您可以在主查询的training_year部分中使用别名的字段WHERE

答案 1 :(得分:0)

您可以在表名称的前面放置别名,并在子查询本身中使用它,如果您要引用外部表,那么最好选择该字段然后与外部表连接。


EDIT v2.0 更改了查询以获得所需的结果

    SELECT YEAR,
       TRAINEES,
       NCT,
       NDT,
       NCT / (NCT + NDT) RATIO
  FROM (  SELECT COUNT (CASE WHEN T.ID_STATUS = 5 THEN 1 END) NCT,
                 COUNT (CASE WHEN T.ID_STATUS IN (6, 7) THEN 1 END) NDT,
                 (SELECT COUNT (TI.ID) TRAINEES
                    FROM trainings_inscriptions TI
                   WHERE TI.id_training = T.ID)
                    TRAINEES,
                 DATE_FORMAT (T.date, "%Y") year
            FROM trainings_trainings t
        GROUP BY DATE_FORMAT (T.date, "%Y"))