从子查询中选择最新日期

时间:2019-06-19 08:04:27

标签: mysql sql

因此,我有一个外部查询和一个子查询,该子查询应在外部查询中返回两个日期。问题是子查询返回几行(即不单独返回t_uPlan)。我需要选择行,其中plan_begin_date和plan_end_date是最新的(即返回一个t_uPlan,其中plan_begin_date和plan_end_date是最新的)。但是我不能在AND块中使用此别名。我该怎么做?

SELECT
        surname_nominative as surname,
        name_nominative as name,
        patr_nominative as patr,
        card_birthdate as birthday,
        study_recordbook as recordbook,
        studgroups_number as studgroups,
        study_kurs as course,
        DATE_FORMAT(payment_agreement_begin, '%Y-%m-%d') as PAYMENT_BEGIN_DATE,
        DATE_FORMAT(DATE_ADD(payment_agreement_begin, interval payment_period * 6 MONTH), '%Y-%m-%d') as PAYMENT_END_DATE,
        t_uPlan.plan_begin_date as PLAN_BEGIN_DATE,
        t_uPlan.plan_end_date as PLAN_END_DATE

        from study


        join student.card on card_id = study_card_id
        join name on card_name_id = name_id
        join surname on card_surname_id = surname_id
        join studgroups on studgroups_id = study_studgroups_id
        join payment on payment_study_id = study_id
        join t_uWorkPlanStudy as workPlanStudy on Study = study_id
        join t_uWorkPlan on uWorkPlan_id = WorkPlan
        join
            (
                SELECT
                t_uPlan1.uPlan_id,
                DATE_FORMAT(CONCAT(t_uPlan1.beginYear, '-09-01'), '%Y-%m-%d') as plan_begin_date,
                DATE_FORMAT(
                    DATE_ADD(
                    DATE_ADD(DATE_FORMAT(CONCAT(t_uPlan1.beginYear, '-09-01'), '%Y-%m-%d'),
                    interval t_uPlan1.duration YEAR),
                    interval t_uPlan1.durationMonth MONTH), '%Y-%m-%d') as plan_end_date
                FROM t_uPlan as t_uPlan1
                JOIN t_uPlan as t_uPlan2 on workPlanStudy.Study = student.study.study_id
                AND ( (




                    t_uPlan1.beginYear < t_uPlan2.beginYear




                    ) or (




                    t_uPlan1.beginYear = t_uPlan2.beginYear



                    ) )
             )
            t_uPlan on uPlan_id = Plan_id
        left join patr on card_patr_id = patr_id


WHERE
        payment_close = 0 and
        payment_action = 1 and
        payment_period <> 0


        group by study_id
        order by surname, PAYMENT_BEGIN_DATE

预期结果

+--------------+---------+----------------+------------+------------+--------------+--------+--------------------+------------------+-----------------+---------------+
|   surname    |  name   |      part      |  birthday  | recordbook |  studgroups  | course | PAYMENT_BEGIN_DATE | PAYMENT_END_DATE | PLAN_BEGIN_DATE | PLAN_END_DATE |
+--------------+---------+----------------+------------+------------+--------------+--------+--------------------+------------------+-----------------+---------------+
| Авазов       | Журабек | Узок Угли      | 1995-12-11 |   16328525 | з33231/12    |      3 | 2016-11-01         | 2020-11-01       | 2016-09-01      | 2021-08-01    |
| Алексанян    | Андрей  | Робертович     | 1981-06-06 |   16323013 | з33231/12    |      3 | 2018-04-05         | 2020-10-05       | 2016-09-01      | 2021-08-01    |
| Алиева       | Ёкутжон | Чулпонбек Кизи | 1996-06-25 |   16378532 | з337331/0501 |      3 | 2017-09-01         | 2020-09-01       | 2016-09-01      | 2021-03-01    |
| Аллабердыева | Инара   | Аразбердыевна  | 1998-08-22 |   16378534 | з337331/0101 |      3 | 2016-09-21         | 2020-09-21       | 2016-09-01      | 2021-03-01    |
| Аль-Атейфи   | Мнира   | Таразан        | 1995-02-22 |   15333003 | з33831/1     |      3 | 2017-03-10         | 2020-09-10       | 2016-09-01      | 2021-07-01    |
| Андреев      | Лев     | Анатольевич    | 1996-11-22 |   16372156 | з237333/0030 |      2 | 2016-07-01         | 2020-07-01       | 2017-09-01      | 2021-01-01    |
+--------------+---------+----------------+------------+------------+--------------+--------+--------------------+------------------+-----------------+---------------+

0 个答案:

没有答案