2011-06-05 67 views
0

這是我在這個論壇中的第一篇文章。我通常在搜索時發現我的問題,但這是我第一次需要發佈此問題。任何幫助將不勝感激。MySQL關於數據匹配集合

我有一個名爲 「gjournal_main」

`dateinput` datetime NOT NULL COMMENT 'Input Date', 
`datepaid` date NOT NULL COMMENT 'Date of Invoice', 
`invoiceno` varchar(12) NOT NULL COMMENT 'Invoice #', 
`bookno` varchar(12) NOT NULL, 
PRIMARY KEY (`invoiceno`), 
KEY `invoiceno` (`invoiceno`) 

表,另一個交易detials 「general_journal」(或項目)

`eid` int(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entry ID', 
`tid` varchar(12) NOT NULL COMMENT 'Transaction ID or Inv no', 
`ref` varchar(15) NOT NULL COMMENT 'Reference no', 
`notes` varchar(30) NOT NULL COMMENT 'Narations', 
`accountname` varchar(50) NOT NULL COMMENT 'AC Title', 
`debit` int(12) NOT NULL, 
`credit` int(12) NOT NULL, 
PRIMARY KEY (`eid`), 
KEY `tid` (`tid`), 
KEY `ref` (`ref`) 

我想要做的 「bookno」 和列表搜索所有具有匹配書號的記錄(例如1254)幾個invoiceno可以有一個bookno。 我想要的結果是這樣的....

------------------------------------------------------ 
inputdate |invoiceno |bookno|accounttitle|debit|credit 
------------------------------------------------------ 
2011-05-29 | 1  |1254 |item a  |12 |0 
2011-05-29 | 1  |1254 |item b  |0 |3 
2011-05-29 | 1  |1254 |item b  |1 |3 
          **TOTAL  |13 |6** 
2010-01-06 | 2  |900 |item a  |10 |0 
2010-01-06 | 2  |900 |item b  |50 |0 
2010-01-06 | 2  |900 |item c  |10 |30 
2010-01-06 | 2  |900 |item d  |10 |0 
          **TOTAL  |80 |30** 

------------------------------------------------------ 

我想用借記卡和信用卡的總和上市發票號碼1和後等我怎麼能做到這一點休息一下?

非常感謝

+0

通過彙總看一看組。 – 2011-06-05 11:48:25

回答

0

使用工會堅持一個符合總計個別線路:

select 
    g.inputdate, 
    g.invoiceno, 
    g.bookno, 
    j.accountname, 
    j.debit, 
    j.credit, 
    1 as recordtype /*invoice line*/ 
from 
    gjournal_main g 
    inner join general_journal j on j.tid = g.invoiceno 
union all 
select 
    null, 
    null, 
    g.invoiceno, 
    '** Total', 
    sum(j.debit), 
    sum(j.credit), 
    2 as recordtype /* total */ 
from 
    gjournal_main g 
    inner join general_journal j on j.tid = g.invoiceno 
group by 
    g.invoiceno 
order by 
    bookno, 
    invoiceno, 
    recordtype 
having 
    bookno = 600 
+0

非常感謝Golez Trol。我已經嘗試過這個聲明,並沒有給我我想要的結果。它顯示了符合標準「Bookno」的所有借方和貸方總計。 – aamir 2011-06-06 21:45:50

+0

非常感謝Golez Trol。我已經嘗試過這個聲明,並沒有給我我想要的結果。它顯示了符合標準「Bookno」的所有借方和貸方總計。我也不是很熟悉mysql,所以請原諒我的愚蠢問題。爲什麼必須把我,j.g然後在列名之前點? – aamir 2011-06-06 21:53:27

+0

糟糕。我想我已經在我的答案中加入了這個組。 – GolezTrol 2011-06-06 22:06:19