2012-06-14 94 views
1

所以我有2個表格,發票和invoiceitems。我想創建一個查詢,將兩個表合併爲一個報表。我遇到的問題是我的查詢只返回來自tblinvoiceitems的第一行,不是所有適用的。看到我的代碼如下。謝謝!在MySQL查詢中合併多行

下面是表:

invoices 
+------+----------+-------------+-------------+------------+----------+-------+---------+ 
| id | userid | duedate | datepaid | subtotal | credit | tax | total | 
+------+----------+-------------+-------------+------------+----------+-------+---------+ 
| 662 | 436  | 2012-07-01 | 2012-06-05 | 1290.00 | 0.00  | 0.00 | 1290.00 | 
+------+----------+-------------+-------------+------------+----------+-------+---------+ 
| 668 | 441  | 2012-07-01 | 2012-06-11 | 1290.00 | 0.00  | 0.00 | 1290.00 | 
+------+----------+-------------+-------------+------------+----------+-------+---------+ 

invoiceitems 
+------+-------------+----------+----------+---------+-----------------+----------+ 
| id | invoiceid | userid | type | relid | description | amount | 
+------+-------------+----------+----------+---------+-----------------+----------+ 
| 1408 | 662   | 436  | Promo | 436  | Setup Fee  | 295.00 | 
+------+-------------+----------+----------+---------+-----------------+----------+ 
| 1409 | 662   | 436  |   | 0  | US Standard  | 995.00 | 
+------+-------------+----------+----------+---------+-----------------+----------+ 
| 1425 | 668   | 441  | Promo | 441  | Setup Fee  | 295.00 | 
+------+-------------+----------+----------+---------+-----------------+----------+ 
| 1426 | 668   | 441  |   | 0  | US Standard  | 995.00 | 
+------+-------------+----------+----------+---------+-----------------+----------+ 

我想表:

newtable 
+------+----------+-------------+-------------+-------------+---------+-----------+------------+ 
| id | userid | invoiceid | duedate | datepaid | setup | hosting | subtotal | 
+------+----------+-------------+-------------+-------------+---------+-----------+------------+ 
| 1 | 436  | 662   | 2012-07-01 | 2012-06-05 | 295.00 | 995.00 | 1290.00 | 
+------+----------+-------------+-------------+-------------+---------+-----------+------------+ 
| 2 | 441  | 668   | 2012-07-01 | 2012-06-11 | 295.00 | 995.00 | 1290.00 | 
+------+----------+-------------+-------------+-------------+---------+-----------+------------+ 

目前查詢:

"INSERT INTO newtable SELECT 
tblinvoices.id, 
tblinvoices.userid, 
tblinvoices.duedate, 
tblinvoices.datepaid, 
tblinvoices.subtotal, 
tblinvoiceitems.invoiceid, 
tblinvoiceitems.amount 
FROM tblinvoices 
INNER JOIN tblinvoiceitems ON tblinvoices.id = tblinvoiceitems.invoiceid"; 
+1

顯示當前的查詢,請。 – Sirko

回答

0

你可以試試:

SELECT 
    i.id AS InvoiceID, i.userid, ii.invoiceid, i.duedate, 
    i.datepaid, i.setup, i.hosting, i.subtotal 
FROM 
    invoices i 
INNER JOIN 
    invoiceitems ii ON i.id = ii.id 
+0

這讓我比我更接近!謝謝Sarfraz! – Drewness

+0

@TGxANAHEiiMx:不客氣 – Sarfraz

0

使用不同的組合,2 LEFT JOIN的和WHERE子句。

SELECT DISTINCT <***> 
LEFT JOIN invoiceitems ii0 ON ii0.id=i.invoiceid 
LEFT JOIN invoiceitems ii1 ON ii1.id=i.invoiceid 
WHERE ii0.description = 'Setup Fee' 
    ANN ii1.description = 'US Standard' 

此外,你最好創建一個單獨的說明表,descriptionInvoiceItemRelation表。

http://en.wikipedia.org/wiki/Database_normalization

+0

在他的問題中,我沒有看到「設置費用」和「美國標準」要求,在他的表格中也沒有「說明」字段。 – Sarfraz

+0

@Sarfraz這就是我看到'setup'和'hosting'列的依賴關係。現在我發現它很可能是最可靠的。 –