0
我有兩個表格。 They are in Heidi 我需要表所示:Rezult如何優化內連接
我嘗試下一個選擇:
SELECT accounts.name, accounts.account_id,date,
(case when accounts.account_id = results.account_id then date end) AS Dat,
results.account_id
FROM results
INNER JOIN accounts
group by date,dat,name
order by accounts.account_id ,date
但我不需要的字段。幫助擺脫他們。
此外: 我必須證明表是這樣的:
--------------------01.2010----02.2010-----03.2010---nextDate
Revenues ----------'x'------------'x'---------'null'---------------------
Personal Costs-----'x'------------'x'---------'x'---------------------
Amortisation---------'x'------------'x'---------'null'---------------------
Summa---------------'3x'-----------'3x'---------'1x'---------------------
我用下一個選擇:
SELECT
GROUP_CONCAT(revenues SEPARATOR ';')as Renenues,
GROUP_CONCAT(personal SEPARATOR ';')as Personal,
GROUP_CONCAT(amortisation SEPARATOR ';')as Amortisation
from (SELECT date,
sum(case when accounts.account_id = 1 then 1 else 0 end) as revenues,
sum(case when accounts.account_id = 2 then 1 else 0 end) as personal ,
sum(case when accounts.account_id = 3 then 1 else 0 end) as amortisation
from accounts
left JOIN results on accounts.account_id = results.account_id
group by date
) t
UNION ALL
SELECT
sum(case when results.account_id = 1 then 1 ELSE 0 end) ,
sum(case when results.account_id = 2 then 1 else 0 end) ,
sum(case when results.account_id = 3 then 1 else 0 end)
from results
然後,我將展示錶行從我的字符串: 'GROUP_CONCAT'。 可能使這樣的SELECT不知道'accounts.account_id'的數量或優化這個SELECT? 我嘗試了第一個SELECT,但得到了很多不必要的信息。
它不起作用。當某些帳戶沒有「日期」時,我需要'空'的名字。名稱 – user3205692
@ user3205692,然後從INNER JOIN更改爲LEFT JOIN – DRapp
@ user3205692,在這種情況下使用LEFT OUTER JOIN。請參閱編輯答案。 – Rahul