在MySql

时间:2017-10-03 11:35:52

标签: mysql

我有以下代码并且以前使用过它,但是想知道是否有人有更好的方法。

基本上我试图从一组带有日期戳的数据中返回结果,我需要将结果格式化为当前年份的几个月。

SELECT * FROM
    (
    SELECT 
    1 AS `month`, YEAR(NOW()) AS `year`, CONCAT(YEAR(NOW()),'-01-01') AS `from`, CONCAT(YEAR(NOW()),'-01-31') AS `to`, 'January' AS `fullMonth`, 'Jan' AS `shortMonth`
    UNION SELECT 2 AS `month`, YEAR(NOW()) AS `year` ,CONCAT(YEAR(NOW()), '-02-01') AS `from`, CONCAT(YEAR(NOW()), '-02-', DAY(LAST_DAY(CONCAT(YEAR(NOW()), '-02-01')))) AS `to`, 'February' AS `fullMonth`, 'Feb' AS `shortMonth`
    UNION SELECT 3 AS `month`, YEAR(NOW()) AS `year`, CONCAT(YEAR(NOW()), '-03-01') AS `from`, CONCAT(YEAR(NOW()), '-03-31') AS `to`, 'March' AS `fullMonth`, 'Mar' AS `shortMonth`
    UNION SELECT 4 AS `month`, YEAR(NOW()) AS `year`, CONCAT(YEAR(NOW()), '-04-01') AS `from`, CONCAT(YEAR(NOW()), '-04-30') AS `to`, 'April' AS `fullMonth`, 'Apr' AS `shortMonth`
    UNION SELECT 5 AS `month`, YEAR(NOW()) AS `year`, CONCAT(YEAR(NOW()), '-05-01') AS `from`, CONCAT(YEAR(NOW()), '-05-31') AS `to`, 'May' AS `fullMonth`, 'May' AS `shortMonth`
    UNION SELECT 6 AS `month`, YEAR(NOW()) AS `year`, CONCAT(YEAR(NOW()), '-06-01') AS `from`, CONCAT(YEAR(NOW()), '-06-30') AS `to`, 'June' AS `fullMonth`, 'Jun' AS `shortMonth`
    UNION SELECT 7 AS `month`, YEAR(NOW()) AS `year`, CONCAT(YEAR(NOW()), '-07-01') AS `from`, CONCAT(YEAR(NOW()), '-07-31') AS `to`, 'July' AS `fullMonth`, 'Jul' AS `shortMonth`
    UNION SELECT 8 AS `month`, YEAR(NOW()) AS `year`, CONCAT(YEAR(NOW()), '-08-01') AS `from`, CONCAT(YEAR(NOW()), '-08-31') AS `to`, 'August' AS `fullMonth`, 'Aug' AS `shortMonth`
    UNION SELECT 9 AS `month`, YEAR(NOW()) AS `year`, CONCAT(YEAR(NOW()), '-09-01') AS `from`, CONCAT(YEAR(NOW()), '-09-30') AS `to`, 'September' AS `fullMonth`, 'Sep' AS `shortMonth`
    UNION SELECT 10 AS `month`, YEAR(NOW()) AS `year`, CONCAT(YEAR(NOW()), '-10-01') AS `from`, CONCAT(YEAR(NOW()) ,'-10-31') AS `to`, 'October' AS `fullMonth`, 'Oct' AS `shortMonth`
    UNION SELECT 11 AS `month`, YEAR(NOW()) AS `year`, CONCAT(YEAR(NOW()), '-11-01') AS `from`, CONCAT(YEAR(NOW()), '-11-30') AS `to`, 'November' AS `fullMonth`, 'Nov' AS `shortMonth`
    UNION SELECT 12 AS `month`, YEAR(NOW()) AS `year`, CONCAT(YEAR(NOW()), '-12-01') AS `from`, CONCAT(YEAR(NOW()), '-12-31') AS `to`, 'December' AS `fullMonth`, 'Dec' AS `shortMonth`
    ) AS `dateFilter`

2 个答案:

答案 0 :(得分:0)

在我的脑海中,您存储了大量冗余信息。考虑使用下表:

(
    SELECT CONCAT(YEAR(NOW()), '-01-01') AS `from`  UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-02-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-03-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-04-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-05-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-06-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-07-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-08-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-09-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-10-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-11-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-12-01')
)

然后,您可以使用MySQL日期函数轻松生成所需的所有其他列,例如

SELECT
    MONTH(`from`)             AS month,
    YEAR(`from`)              AS year,
    `from`                    AS first_day,
    LAST_DAY(`from`)          AS last_day,
    DATE_FORMAT(`from`, '%M') AS full_month,
    DATE_FORMAT(`from`, '%b') AS short_month
FROM yourTable;

答案 1 :(得分:0)

很好,他的答案可达Tim Biegeleisen。我交换了以下更简单的代码。

SELECT
    MONTH(`from`) AS `month`,
    YEAR(`from`) AS `year`,
    `from` AS `from`,
    LAST_DAY(`from`) AS `to`,
    DATE_FORMAT(`from`, '%M') AS `fullMonth`,
    DATE_FORMAT(`from`, '%b') AS `shortMonth`
FROM 
(
    SELECT CONCAT(YEAR(NOW()), '-01-01') AS `from`  UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-02-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-03-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-04-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-05-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-06-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-07-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-08-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-09-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-10-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-11-01') UNION ALL
    SELECT CONCAT(YEAR(NOW()), '-12-01')
) AS `range`