2015-10-20 73 views
-1

我在mysql查詢中正在創建兩列。他們應該看起來像這樣。在mysql中動態創建兩列

+------------------+ 
|Month | toOrder| 
+------------------- 
|Nov-2014 |2014-11 | 
-------------------- 
|Dec-2014 |2014-12 | 
-------------------- 
|Jan-2015 |2015-01 | 
-------------------- 
|Feb-2015 |2015-02 | 
-------------------- 
|Mar-2015 |2015-03 | 
-------------------- 
|Apr-2015 |2015-04 | 
-------------------- 
|May-2015 |2015-05 | 
-------------------- 
|Jun-2015 |2015-06 | 
-------------------- 
|Jul-2015 |2015-07 | 
-------------------- 
|Aug-2015 |2015-08 | 
-------------------- 
|Sep-2015 |2015-09 | 
-------------------- 
|Oct-2015 |2015-10 | 
+------------------+ 

我使用此查詢

SELECT * 
FROM (SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 11 MONTH), '%b-%Y') as Month UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 10 MONTH), '%b-%Y') as Month UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 9 MONTH), '%b-%Y') as Month UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 8 MONTH), '%b-%Y') as Month UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 7 MONTH), '%b-%Y') as Month UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 6 MONTH), '%b-%Y') as Month UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 5 MONTH), '%b-%Y') as Month UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 4 MONTH), '%b-%Y') as Month UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 3 MONTH), '%b-%Y') as Month UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 2 MONTH), '%b-%Y') as Month UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 1 MONTH), '%b-%Y') as Month UNION 
    SELECT DATE_FORMAT(now(), '%b-%Y') as Month) AS Months 
LEFT JOIN 
    (SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 11 MONTH), '%Y-%m') as toOrder UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 10 MONTH), '%Y-%m') as toOrder UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 9 MONTH), '%Y-%m') as toOrder UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 8 MONTH), '%Y-%m') as toOrder UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 7 MONTH), '%Y-%m') as toOrder UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 6 MONTH), '%Y-%m') as toOrder UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 5 MONTH), '%Y-%m') as toOrder UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 4 MONTH), '%Y-%m') as toOrder UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 3 MONTH), '%Y-%m') as toOrder UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 2 MONTH), '%Y-%m') as toOrder UNION 
    SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 1 MONTH), '%Y-%m') as toOrder UNION 
    SELECT DATE_FORMAT(now(), '%Y-%m') as toOrder) AS toOrders 
ON TRUE = TRUE 

但是我收到的輸出是什麼,我期待的不同。

+------------------+ 
|Month | toOrder| 
+------------------- 
|Nov-2014 |2014-11 | 
-------------------- 
|Dec-2014 |2014-11 | 
-------------------- 
|Jan-2015 |2014-11 | 
-------------------- 
|Feb-2015 |2014-11 | 
-------------------- 
|Mar-2015 |2014-11 | 
-------------------- 
|Apr-2015 |2014-11 | 
-------------------- 
|May-2015 |2014-11 | 
-------------------- 
|Jun-2015 |2014-11 | 
-------------------- 
|Jul-2015 |2014-11 | 
-------------------- 
|Aug-2015 |2014-11 | 
-------------------- 
|Sep-2015 |2014-11 | 
-------------------- 
|Oct-2015 |2014-11 | 
-------------------- 
|Nov-2014 |2014-12 | 
-------------------- 
|Dec-2014 |2014-12 | 
-------------------- 
|Jan-2015 |2014-12 | 
-------------------- 
|Feb-2015 |2014-12 | 
-------------------- 
|Mar-2015 |2014-12 | 
-------------------- 
|Apr-2015 |2014-12 | 
-------------------- 
|May-2015 |2014-12 | 
-------------------- 
|Jun-2015 |2014-12 | 
-------------------- 
|Jul-2015 |2014-12 | 
-------------------- 
|Aug-2015 |2014-12 | 
-------------------- 
|Sep-2015 |2014-12 | 
-------------------- 
|Oct-2015 |2014-12 | 
-------------------- 
|Nov-2014 |2015-01 | 
+------------------+ 

我使用「toOrders」欄目,以做一個ORDER BY後與其他LEFT JOIN,我將在未來。

+0

歡迎來到堆棧溢出。在這裏真正提出一個問題很重要。你有兩個重要的元素:1)我想要這個。 2)我試過了。但你沒有3),但它產生的,而不是我想要的。 –

+0

@Ollie謝謝您的評論,我會將第三個元素添加到我的問題中以獲得完整的問題。 –

回答

0

你正在做一個沒有ON子句的JOIN。這稱爲交叉連接,並生成所有可能的輸出組合。像這樣的東西可能會產生你想要的東西。

SELECT DATE_FORMAT(Month,'%b-%Y') AS Month, 
     DATE_FORMAT(Month,'%Y-%m') AS toOrder 
FROM (
    SELECT DATE_SUB(now(), INTERVAL 11 MONTH) as Month  UNION ALL 
    SELECT DATE_SUB(now(), INTERVAL 10 MONTH) as Month  UNION ALL 
    SELECT DATE_SUB(now(), INTERVAL 9 MONTH) as Month  UNION ALL 
    SELECT DATE_SUB(now(), INTERVAL 8 MONTH) as Month  UNION ALL 
    SELECT DATE_SUB(now(), INTERVAL 7 MONTH) as Month  UNION ALL 
    SELECT DATE_SUB(now(), INTERVAL 6 MONTH) as Month  UNION ALL 
    SELECT DATE_SUB(now(), INTERVAL 5 MONTH) as Month  UNION ALL 
    SELECT DATE_SUB(now(), INTERVAL 4 MONTH) as Month  UNION ALL 
    SELECT DATE_SUB(now(), INTERVAL 3 MONTH) as Month  UNION ALL 
    SELECT DATE_SUB(now(), INTERVAL 2 MONTH) as Month  UNION ALL 
    SELECT DATE_SUB(now(), INTERVAL 1 MONTH) as Month  UNION ALL 
    SELECT DATE_SUB(now(), INTERVAL 0 MONTH) as Month 
) months 
ORDER BY months.Month 

然而這種製劑可產生近幾個月的兩端奇怪的結果。我會更完全地信任以下查詢。

SELECT DATE_FORMAT(month.start - INTERVAL seq.seq MONTH,'%b-%Y') AS Month, 
     DATE_FORMAT(month.start - INTERVAL seq.seq MONTH,'%Y-%m') AS toOrder 
FROM (
    SELECT 11 AS seq UNION ALL 
    SELECT 10 UNION ALL 
    SELECT 9 UNION ALL 
    SELECT 8 UNION ALL 
    SELECT 7 UNION ALL 
    SELECT 6 UNION ALL 
    SELECT 5 UNION ALL 
    SELECT 4 UNION ALL 
    SELECT 3 UNION ALL 
    SELECT 2 UNION ALL 
    SELECT 1 UNION ALL 
    SELECT 0 
) seq 
JOIN (
    SELECT CURRENT_DATE() - INTERVAL DAYOFMONTH(CURRENT_DATE()) - 1 DAY AS start 
) month 
ORDER BY seq.seq DESC 

這是因爲

SELECT CURRENT_DATE() - INTERVAL DAYOFMONTH(CURRENT_DATE()) - 1 DAY AS start 

確實將今天的日期到本月份的第一天的好工作。

+0

非常感謝你的回答。第一種解決方案我會將訂單更改爲ORDER BY toOrder,然後獲得我需要的訂單。第二種解決方案爲我提供了所需的確切輸出。 –

+0

此外,第二種解決方案告訴我,查詢將從今天到12個月後返回。對? –

+0

您在我的第一個解決方案中發現了一個錯誤,謝謝。我會使用'ORDER BY months.Month'。我編輯了我的答案。第二個查詢列出了與當前月份相關的十二個月*結束*的列表。 –