我在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,我將在未來。
歡迎來到堆棧溢出。在這裏真正提出一個問題很重要。你有兩個重要的元素:1)我想要這個。 2)我試過了。但你沒有3),但它產生的,而不是我想要的。 –
@Ollie謝謝您的評論,我會將第三個元素添加到我的問題中以獲得完整的問題。 –