【问题标题】:Subquery as another field子查询作为另一个字段
【发布时间】:2014-01-29 18:39:44
【问题描述】:

我正在尝试执行 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    |                |
+-----------+---------+------+-----+---------+----------------+

【问题讨论】:

    标签: mysql schema invoice


    【解决方案1】:

    尝试下面的查询,如果使用 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
    

    【讨论】:

    • 非常感谢!这完美无缺。它所需要的只是一个 HAVING 子句HAVING info IS NOT NULL。现在我可以继续这个项目了。
    猜你喜欢
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2022-10-15
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多