2012-08-27 56 views
0

我想顯示同一列的三個不同的數字在mysql查詢中,我想保留一個月的靜態:四月,所以它會是這樣的情況我要顯示的當月,比上月和我一起工作的一年中靜月,在這種情況下,我們堅持2012顯示使用mysql表中的前一個和當前月份數據

Example 
Tablename:payment 

    id , pay_date, amount 
    1 2012-02-12 1000 
    2 2012-03-11 780 
    3 2012-04-15 890 
    4 2012-05-12 1200 
    5 2012-06-12 1890 
    6 2012-07-12 1350 
    7 2012-08-12 1450 

所以我想要做的就是顯示列amount爲4月份,因爲我說我想保持這一行靜態:890,當前月份可以說當前月份是8月:1450和上個月將是7月份的金額:1350:因此最終結果會是這樣的:

april_amount current_month_amount previous_month_amount 
    890     1450     1350 

但是我在這裏卡住了:

select amount as april_amount 
from payment 
where monthname(pay_date) LIKE 'April' 
and year(pay_date) LIKE 2012 

我希望這個問題被寫足夠的清楚,非常感謝對大加讚賞的幫助。

回答

0

我可能會離開這裏。但嘗試:

select top 1 
    p.amount, c.amount, n.amount 
from payment c 
    inner join payment p ON p.pay_date < c.pay_date 
    inner join payment n ON n.pay_date > c.pay_date 
where monthname(c.paydate) LIKE 'April' 
    and year(c.pay_date) LIKE 2012 
order by p.pay_date DESC, n.pay_date ASC 

編輯,我沒有正確地讀你的問題。我正在去往,現在和下個月。 1分鐘,我會再試一次。

select top 1 
    p.amount AS april_amount, c.amount AS current_month_amount, n.amount AS previous_month_amount 
from payment c 
    inner join payment p ON monthname(p.pay_date) = 'April' AND year(p.pay_date) = 2012 
    inner join payment n ON n.pay_date > c.pay_date 
where monthname(c.paydate) = monthname(curdate()) 
    and year(c.pay_date) = year(curdate()) 
order by n.pay_date ASC 

這假定每個月只有1個條目。

好的,所以我沒有寫在MySQL一段時間。這裏是您的示例數據什麼工作:

select 
    p.amount AS april_amount, c.amount AS current_month_amount, n.amount AS previous_month_amount 
from payment AS c 
    inner join payment AS p ON monthname(p.pay_date) LIKE 'April' AND year(p.pay_date) LIKE 2012 
    inner join payment AS n ON n.pay_date < c.pay_date 
where monthname(c.pay_date) LIKE monthname(curdate()) 
    and year(c.pay_date) LIKE year(curdate()) 
order by n.pay_date DESC 
limit 1 

比上月表連接的直覺相反命名n,但這個工程。我在WAMP安裝中驗證了它。

要處理每月的聚合,您可以使用子選擇。性能可能會受到非常大的表格影響(數百萬行或更多)。

SELECT SUM(a.amount) AS april_amount, 
(
    SELECT SUM(c.amount) 
    FROM payment c 
    WHERE MONTH(c.pay_date) = MONTH(CURDATE()) 
) AS current_month_amount, 
(
    SELECT SUM(p.amount) 
    FROM payment p 
    WHERE MONTH(p.pay_date) = MONTH(CURDATE() - INTERVAL 1 
    MONTH) 
) AS previous_month_amount 
FROM payment a 
WHERE MONTHNAME(a.pay_date) = 'April' 
AND YEAR(a.pay_date) =2012 
+0

如果每個月有幾個條目並且迄今爲止感謝一百萬的情況下會怎麼樣,生病嘗試並讓您知道結果? – dames

+0

'previous_month_amount'會更準確地命名爲'previous_payment_amount' –

+0

我創建了表格並意識到我的錯誤。在這裏你去: –

1

如果其結果可能是行,而不是列:

SELECT MONTHNAME(pay_date), amount FROM payment 

WHERE pay_date BETWEEN '2012-04-01' 
        AND '2012-04-30' 

    OR pay_date BETWEEN CURRENT_DATE 
         - INTERVAL DAYOFMONTH(CURRENT_DATE) - 1 DAY 
        AND LAST_DAY(CURRENT_DATE) 

    OR pay_date BETWEEN CURRENT_DATE 
         - INTERVAL DAYOFMONTH(CURRENT_DATE) - 1 DAY 
         - INTERVAL 1 MONTH 
        AND LAST_DAY(CURRENT_DATE - INTERVAL 1 MONTH) 

見它sqlfiddle

+0

有沒有辦法,我可以得到它在列 – dames

+0

我使用列,因爲我不只是與一行數據,但幾個,我只是使用上述表作爲一個小例子 – dames

+0

@dames:你能否詳細說明什麼其他行包含? – eggyal

相關問題