如何将子查询中的字符串连接到 MySQL 中的单行中?

发布于 2024-09-27 08:04:01 字数 1681 浏览 7 评论 0原文

我有三个表:

table "package"
-----------------------------------------------------
package_id      int(10)        primary key, auto-increment
package_name    varchar(255)
price           decimal(10,2)

table "zones"
------------------------------------------------------
zone_id         varchar(32)    primary key (ex of data: A1, Z2, E3, etc)

table "package_zones"
------------------------------------------------------
package_id     int(10)
zone_id        varchar(32)

我想要做的是返回包表中的所有信息以及该包的区域列表。我想要按字母顺序排序并以逗号分隔的区域列表。

所以我正在寻找的输出是这样的:

+------------+---------------+--------+----------------+
| package_id | package_name  | price  | zone_list      |
+------------+---------------+--------+----------------+
| 1          | Red Package   | 50.00  | Z1,Z2,Z3       |
| 2          | Blue Package  | 75.00  | A2,D4,Z1,Z2    |
| 3          | Green Package | 100.00 | B4,D1,D2,X1,Z1 |
+------------+---------------+--------+----------------+

我知道我可以在 PHP 中使用表示层做一些事情来获得所需的结果。问题是,我希望能够对 zone_list ASCDESC 进行排序,甚至使用 WHERE zone_list LIKE 等。为此,我需要在 MySQL 中完成此操作。

我什至不知道如何开始解决这个问题。我尝试使用子查询,但它一直抱怨多行。我尝试将多行连接成一个字符串,但显然 MySQL 不喜欢这样。

提前致谢。


更新!

对于有兴趣的人来说,这是解决方案:

SELECT 
    `package`.*,
    GROUP_CONCAT(`zones`.`zone` ORDER BY `zones`.`zone` ASC SEPARATOR ','  )  as `zone_list`
FROM 
    `package`,
    `package_zones`
LEFT JOIN 
    (`zones`,`ao_package_zones`) ON (`zones`.`zone_id` = `package_zones`.`zone_id` AND `package_zones`.`package_id` = `package`.`package_id`)
GROUP BY 
    `ao_package`.`package_id`

I have three tables:

table "package"
-----------------------------------------------------
package_id      int(10)        primary key, auto-increment
package_name    varchar(255)
price           decimal(10,2)

table "zones"
------------------------------------------------------
zone_id         varchar(32)    primary key (ex of data: A1, Z2, E3, etc)

table "package_zones"
------------------------------------------------------
package_id     int(10)
zone_id        varchar(32)

What I'm trying to do is return all the information in package table PLUS a list of zones for that package. I want the list of zones sorted alphabetically and comma separated.

So the output I'm looking for is something like this:

+------------+---------------+--------+----------------+
| package_id | package_name  | price  | zone_list      |
+------------+---------------+--------+----------------+
| 1          | Red Package   | 50.00  | Z1,Z2,Z3       |
| 2          | Blue Package  | 75.00  | A2,D4,Z1,Z2    |
| 3          | Green Package | 100.00 | B4,D1,D2,X1,Z1 |
+------------+---------------+--------+----------------+

I know I could do something in PHP with the presentation layer to get the desired result. The problem is, I would like to be able to sort zone_list ASC or DESC or even use WHERE zone_list LIKE and so on. In order to do that, I need this done in MySQL.

I have NO idea how to even begin to tackle this. I tried using a subquery, but it kept complaining about multiple rows. I tried to concat the multiple rows into a single string, but evidently MySQL doesn't like this.

Thanks in advance.


UPDATE!

Here is the solution for those who are interested:

SELECT 
    `package`.*,
    GROUP_CONCAT(`zones`.`zone` ORDER BY `zones`.`zone` ASC SEPARATOR ','  )  as `zone_list`
FROM 
    `package`,
    `package_zones`
LEFT JOIN 
    (`zones`,`ao_package_zones`) ON (`zones`.`zone_id` = `package_zones`.`zone_id` AND `package_zones`.`package_id` = `package`.`package_id`)
GROUP BY 
    `ao_package`.`package_id`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

束缚m 2024-10-04 08:04:01

通过使用 GROUP_CONCAT() 函数和 GROUP BY 调用。这是一个示例查询,

SELECT 
   p.package_id,
   p.package_name,
   p.price,
   GROUP_CONCAT(pz.zone_id SEPARATOR ',') as zone_list 
FROM 
   package p 
LEFT JOIN package_zone pz ON p.package_id = pz.package_id 
GROUP BY 
   p.package_id

您应该仍然可以按 zone_id (或 zone_list)进行排序,并且可以使用 WHERE zp.zone_id = 'Z1' 而不是使用 LIKE或类似的东西。

by using the GROUP_CONCAT() function and a GROUP BY call. here's an example query

SELECT 
   p.package_id,
   p.package_name,
   p.price,
   GROUP_CONCAT(pz.zone_id SEPARATOR ',') as zone_list 
FROM 
   package p 
LEFT JOIN package_zone pz ON p.package_id = pz.package_id 
GROUP BY 
   p.package_id

you should still be able to order by zone_id s (or zone_list), and instead of using LIKE, you can use WHERE zp.zone_id = 'Z1' or something similar.

无声无音无过去 2024-10-04 08:04:01

我有一位客户要求我生成一个查询,以便在 prestashop 数据库中使用,以从 ps_ordersps_customer 表中获取去年的记录;其中一列是 ps_order_detail 表中的“订单中的产品 ID 列表”。所以,我的查询结果是:

SELECT 
    o.id_order, o.reference, 
    (
        SELECT 
            GROUP_CONCAT(od.product_id SEPARATOR ', ') AS products 
        FROM ps_order_detail od 
        WHERE od.id_order = o.id_order 
        GROUP BY od.id_order
    ) AS product_ids,
    CONCAT(c.firstname, ' ', c.lastname) AS customer, o.current_state, o.payment, o.total_discounts, 
    o.total_discounts_tax_incl, o.total_discounts_tax_excl, o.total_paid, o.total_paid_tax_incl,
    o.total_paid_tax_excl, o.total_products, o.total_products_wt, o.total_shipping, o.total_shipping_tax_incl, 
    o.total_shipping_tax_excl, o.carrier_tax_rate, o.total_wrapping, o.total_wrapping_tax_incl, 
    o.total_wrapping_tax_excl, o.invoice_number, o.delivery_number, o.valid
FROM ps_orders o
INNER JOIN ps_customer c ON c.id_customer = o.id_customer
WHERE 
    o.date_add <= NOW() AND o.date_add >= DATE_ADD(NOW(), INTERVAL - 12 MONTH);

结果:

在此处输入图像描述

I have a customer who requested me to generate a query to use in a prestashop database to get the last year records from ps_orders, ps_customer tables; and one of those column was "The list of product ids in the order" from the ps_order_detail table. So, my query result was:

SELECT 
    o.id_order, o.reference, 
    (
        SELECT 
            GROUP_CONCAT(od.product_id SEPARATOR ', ') AS products 
        FROM ps_order_detail od 
        WHERE od.id_order = o.id_order 
        GROUP BY od.id_order
    ) AS product_ids,
    CONCAT(c.firstname, ' ', c.lastname) AS customer, o.current_state, o.payment, o.total_discounts, 
    o.total_discounts_tax_incl, o.total_discounts_tax_excl, o.total_paid, o.total_paid_tax_incl,
    o.total_paid_tax_excl, o.total_products, o.total_products_wt, o.total_shipping, o.total_shipping_tax_incl, 
    o.total_shipping_tax_excl, o.carrier_tax_rate, o.total_wrapping, o.total_wrapping_tax_incl, 
    o.total_wrapping_tax_excl, o.invoice_number, o.delivery_number, o.valid
FROM ps_orders o
INNER JOIN ps_customer c ON c.id_customer = o.id_customer
WHERE 
    o.date_add <= NOW() AND o.date_add >= DATE_ADD(NOW(), INTERVAL - 12 MONTH);

Result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文