MySQL View 1:许多表在一行中显示所有相关记录

时间:2011-07-01 23:00:17

标签: mysql view row

我需要一个MySQL视图,为每个发票号和所有相关记录返回一行

发票

--------------------------------------------
|InvoiceID| CustomerID | InvoiceDetailsID  |
--------------------------------------------
| 1       | 1001       | 2200              |
--------------------------------------------
| 2       | 2000       | 2100              |
--------------------------------------------

详细

-----------------------------------------------
| ID      | DetailsID  | Description | Amount |
-----------------------------------------------
| 1       |   2200     | 10211       |  22.00 |
-----------------------------------------------
| 2       |   2200     | 10212       |  12.00 |
-----------------------------------------------
| 3       |   2200     | 10922       |  99.00 |
-----------------------------------------------
| 4       |   2100     | 99111       |   1.00 |
-----------------------------------------------
| 5       |   2100     | 88229       |   4.50 |
-----------------------------------------------

需要返回以下内容(详细信息表可能每个DetailsID有3条以上的记录,但我只需要显示 每个DetailsID的前三个基于ID)

------------------------------------------------------------------------------------------------------------------------------
| InvoiceID | CustomerID | InvoiceDetailsID | Description_1 | Amount_1 | Description_2 | Amount_2 | Description_3 | Amount_3 | 
-    -----------------------------------------------------------------------------------------------------------------------------
| 1         | 1001       | 2200             | 10211         | 22.00    | 10212         | 12.00    | 10922         | 99.00    |
------------------------------------------------------------------------------------------------------------------------------
| 2         | 2000       | 2100             | 99111         |  1.00    | 88229         |  4.50    | NULL          | NULL     |
------------------------------------------------------------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

有点难看,不是吗?

SELECT
    d.InvoiceID
  , d.CustomerID 
  , d.InvoiceDetailsID
  , ( SELECT i.Description  FROM InvoiceDetails i
      WHERE i.DetailsID = d.InvoiceDetailsID 
      ORDER BY i.id   LIMIT 0,1
    ) AS Description_1 
  , ( SELECT i.Amount       FROM InvoiceDetails i
      WHERE i.DetailsID = d.InvoiceDetailsID 
      ORDER BY i.id   LIMIT 0,1
    ) AS Amount_1
  , ( SELECT i.Description  FROM InvoiceDetails i
      WHERE i.DetailsID = d.InvoiceDetailsID 
      ORDER BY i.id   LIMIT 1,1
    ) AS Description_2 
  , ( SELECT i.Amount       FROM InvoiceDetails i
      WHERE i.DetailsID = d.InvoiceDetailsID 
      ORDER BY i.id   LIMIT 1,1
    ) AS Amount_2
  , ( SELECT i.Description  FROM InvoiceDetails i
      WHERE i.DetailsID = d.InvoiceDetailsID 
      ORDER BY i.id   LIMIT 2,1
    ) AS Description_3 
  , ( SELECT i.Amount       FROM InvoiceDetails i
      WHERE i.DetailsID = d.InvoiceDetailsID 
      ORDER BY i.id   LIMIT 2,1
    ) AS Amount_3
FROM Details AS d
ORDER BY d.InvoiceDetailsID