所以我有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";
顯示當前的查詢,請。 – Sirko