嗨,我是mysql新手
Address Type MonthCreated YearCreated
AddA1 M1 August 2018
AddA1 I2 August 2018
AddA1 K2 October 2018
所需输出 就像这样,从1月2月到2018年12月,每个月都会分配三行
Select AddA1,Type,MonthCreated,YearCreated
from tbltry
where YearCreated = '2018'
所需的输出是每个月的所有行,因为它仍在同一年,我只是想知道它是否可能真的需要帮助或建议
Address Type MonthCreated YearCreated
AddA1 M1 January 2018
AddA1 I2 January 2018
AddA1 K2 January 2018
AddA1 M1 February 2018
AddA1 I2 February 2018
AddA1 K2 February 2018
AddA1 M1 March 2018
AddA1 I2 March 2018
AddA1 K2 March 2018
直到12月,它会返回所有行吗?真的需要建议
答案 0 :(得分:0)
AddA1
和Type
在2018年的不同组合。CROSS JOIN
即可获得所有可能的月份。尝试:
SELECT dttry.*, all_months.*
FROM
(SELECT DISTINCT AddA1, Type, YearCreated
FROM tbltry
WHERE YearCreated = '2018') AS dttry
CROSS JOIN
(SELECT 'January' AS MonthCreated UNION ALL
SELECT 'February' UNION ALL
SELECT 'March' UNION ALL
SELECT 'April' UNION ALL
SELECT 'May' UNION ALL
SELECT 'June' UNION ALL
SELECT 'July' UNION ALL
SELECT 'August' UNION ALL
SELECT 'September' UNION ALL
SELECT 'October' UNION ALL
SELECT 'November' UNION ALL
SELECT 'December') AS all_months