子查询作为另一个字段

时间:2014-01-29 18:39:44

标签: mysql schema invoice

我试图进行SELECT查询....而不是UPDATE或INSERT或DELETE。

我有三张桌子。

  • 客户表
  • 发票表
  • invoice_items表

我想运行一个查询,向我显示每张发票。每张发票只能有一个客户和多个商品......因此存在invoice_items

我当前的查询看起来像这样

SELECT i.order_date, c.name, thedata.info from invoices i inner join customers c ON (i.customer = c.id) right join ( select x.order, group_concat( concat(x.itemname,' ', x.itemdesc) separator "\n" ) as info from invoice_items x ) thedata on (i.id = thedata.order)

当我运行此查询时,我会收到一行,其中包含一个客户,一个发票以及每个项目的列表,无论发票ID或客户是谁...... ???

+---------------------+--------------+---------------------------------------------------------------------------------------------------------------------------------+
| order_date          | name         | info                                                                                                                            |
+---------------------+--------------+---------------------------------------------------------------------------------------------------------------------------------+
| 2014-01-23 20:39:20 | Joe Customer | Boxes for boxing
Shoes for shining
2" Hermosa Plank for bobblin
Boxes for boxing
bobbles for bobblin
Lot 297 Woodale Carmel Oak |
+---------------------+--------------+---------------------------------------------------------------------------------------------------------------------------------+

我的目标是获得相同的列表,但显示所有客户以及他们的项目。 我做错了什么?

以下是那些需要它们的模式。

客户

+---------------+------------+------+-----+---------+----------------+
| Field         | Type       | Null | Key | Default | Extra          |
+---------------+------------+------+-----+---------+----------------+
| id            | int(11)    | NO   | PRI | NULL    | auto_increment |
| name          | text       | NO   |     | NULL    |                |
| ship_address  | text       | NO   |     | NULL    |                |
| ship_address2 | text       | NO   |     | NULL    |                |
| ship_city     | text       | NO   |     | NULL    |                |
| ship_state    | text       | NO   |     | NULL    |                |
| ship_zip      | int(6)     | NO   |     | NULL    |                |
| bill_address  | text       | NO   |     | NULL    |                |
| bill_address2 | text       | NO   |     | NULL    |                |
| bill_city     | text       | NO   |     | NULL    |                |
| bill_state    | text       | NO   |     | NULL    |                |
| bill_zip      | text       | NO   |     | NULL    |                |
| phone         | bigint(20) | NO   |     | NULL    |                |
| email         | text       | NO   |     | NULL    |                |
+---------------+------------+------+-----+---------+----------------+

发票

+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+
| id          | int(11)  | NO   | PRI | NULL    | auto_increment |
| customer    | int(11)  | NO   |     | NULL    |                |
| order_date  | datetime | NO   |     | NULL    |                |
| status      | text     | NO   |     | NULL    |                |
| freightcost | double   | NO   |     | NULL    |                |
+-------------+----------+------+-----+---------+----------------+

Invoice_items

+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| id        | int(11) | NO   | PRI | NULL    | auto_increment |
| order     | int(11) | NO   |     | NULL    |                |
| qty       | int(11) | NO   |     | NULL    |                |
| itemname  | text    | NO   |     | NULL    |                |
| itemdesc  | text    | NO   |     | NULL    |                |
| itemprice | double  | NO   |     | NULL    |                |
+-----------+---------+------+-----+---------+----------------+

1 个答案:

答案 0 :(得分:0)

尝试以下查询,如果使用GROUP_CONCAT(),则需要使用GROUP BY。

SELECT i.order_date,
       c.name,
       group_concat( concat(x.itemname,' ', x.itemdesc) separator "\n" ) as info
FROM invoices i 
INNER JOIN customers c ON i.customer = c.id
LEFT JOIN invoice_items x ON i.id = x.order
GROUP BY i.order_date,c.name