1

したがって、請求書と請求書の2つのテーブルがあります。レポート用に 2 つを 1 つのテーブルに結合するクエリを作成したいと考えています。私が直面している問題は、クエリが tblinvoiceitems から最初の行のみを返すことであり、すべてが当てはまるわけではありません。以下の私のコードを参照してください。ありがとう!

ここにテーブルがあります:

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";
4

2 に答える 2

0

別個の2つのLEFTJOINと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'

また、description用に個別のテーブルとdescriptionInvoiceItemRelationテーブルを作成することをお勧めします。

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

于 2012-06-14T15:17:11.010 に答える
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
于 2012-06-14T15:18:31.860 に答える