我有 3 个子查询,它们可以单独正常工作。我正试图加入他们与 Union All。查询运行并给出了正确数量的结果,但是日期字段的格式被搞乱了;例如“2016-11-01”显示为“2,016, -11, -01”。这对每一行都是一样的。
我进行了搜索,发现了一些与别名有关的线程,但它们似乎不是这里的问题,因为查询运行良好,日期格式除外。
任何帮助将不胜感激,谢谢。
这是代码:
# offers_8a
SELECT advisor, ats_id, offer_date
FROM max_offers
WHERE offer_date > (CURDATE()- INTERVAL 135 DAY)
GROUP BY advisor, ats_id, offer_date
UNION ALL
# offers_8b
SELECT mo.advisor, mo.ats_id, mo.offer_date
FROM max_offers as mo
INNER JOIN source_tables.ats as ats
ON mo.ats_id = ats.ats_id
INNER JOIN source_tables.intake_corrections as ic
ON ats.intake = ic.sugar_name
INNER JOIN source_tables.intakes as it
ON ic.intake = it.intake
INNER JOIN source_tables.dates as dt
ON ic.intake = dt.current_intake
GROUP BY mo.advisor, mo.ats_id, mo.offer_date, it.adms_intake_start
HAVING offer_date > (it.adms_intake_start - INTERVAL 135 DAY)
UNION ALL
# offers_8c
SELECT mo.advisor, mo.ats_id, mo.offer_date
FROM max_offers as mo
INNER JOIN source_tables.ats as ats
ON mo.ats_id = ats.ats_id
INNER JOIN source_tables.intake_corrections as ic
ON ats.intake = ic.sugar_name
INNER JOIN source_tables.intakes as it
ON ic.intake = it.intake
INNER JOIN
#s_prev_intake
(SELECT current_next_intakes.current_intake, assign, forecast_intake, forecast_intake_sugar
FROM source_tables.dates
INNER JOIN source_tables.current_next_intakes
ON source_tables.dates.current_intake = source_tables.current_next_intakes.current_intake
WHERE assign = 'Prev') AS prv
ON ic.intake = prv.forecast_intake
GROUP BY mo.advisor, mo.ats_id, mo.offer_date, it.adms_intake_start
HAVING offer_date > (it.adms_intake_start - INTERVAL 135 DAY);
我通过将 Union 括在括号中并将其用作 Select 查询中的派生表来解决此问题,不需要更改任何格式等。
使用 Union 查询总是最好的吗?我在网上找到的例子没有说明这一点。
谢谢
使用
UNION
而不是UNION ALL
。