2014-01-29 84 views
0

我试图做一个SELECT查询....不是更新或INSERT或DELETE。子查询作为另一个字段

我有三张桌子。

  • Customers表
  • 发票表
  • 的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) 

当我运行此查询,我收到一封包含一个行,一个客户存在,一张发票,以及任何每件商品的清单,无论发票编号或客户... ???

+---------------------+--------------+---------------------------------------------------------------------------------------------------------------------------------+ 
| 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 |    | 
+-----------+---------+------+-----+---------+----------------+ 

回答

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 
+0

非常感谢!这工作完美无瑕。它只需要一个HAVING子句'HAVING info IS NOT NULL'。现在我可以继续这个项目。 – dockeryZ

+0

很高兴为雅工作 –

相关问题