我在MySQL 8中有3个相关表:

CREATE TABLE `products` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `status` enum('D','P','A','I') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'D' COMMENT ' D => Draft, P=>Pending Review, A=>Active, I=>Inactive',
  `sale_price` int unsigned DEFAULT NULL,
...


CREATE TABLE `discount_product` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `product_id` bigint unsigned NOT NULL,
  `discount_id` tinyint unsigned NOT NULL,
...  
  

CREATE TABLE `discounts` (
  `id` tinyint unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `active` tinyint(1) NOT NULL DEFAULT '0',
  `min_qty` int unsigned DEFAULT NULL,
  `max_qty` int unsigned DEFAULT NULL,
  `percent` decimal(7,2) unsigned NOT NULL,
  `description` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL, 
...

我需要在第2栏中显示相关折扣的产品;

我有两个工作要求.占位符为‘Discount_Info Content’的第一名:

SELECT products.id, products.title, products.sale_price,
    GROUP_CONCAT('discount_info content') AS discount_info

    FROM products WHERE has_discount_price = 1 AND id = 13
    GROUP BY products.id, products.title, products.sale_price

和按产品ID提供的第二次折扣:

SELECT  CONCAT(discounts.name, ': ', discounts.min_qty, ': ', discounts.max_qty, ': ', discounts.percent)
  FROM discounts, discount_product WHERE discount_product.discount_id = discounts.id AND discount_product.product_id = 13

两个请求都可以.

但是,当我使用GROUP_CONCAT将第二个请求插入到第一个请求的‘discount_info content’占位符中时:

SELECT products.id, products.title, products.sale_price,
    GROUP_CONCAT(SELECT  CONCAT(discounts.name, ': ', discounts.min_qty, ': ', discounts.max_qty, ': ', discounts.percent)
  FROM discounts, discount_product WHERE discount_product.discount_id = discounts.id AND discount_product.product_id = 13) AS discount_info


    FROM products WHERE has_discount_price = 1 AND id = 13
GROUP BY products.id, products.title, products.sale_price

我收到错误:

SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM discounts, discount_product WHERE discount_product.discount_id = discounts.' at line 3

如果我从GROUP_CONCAT内部删除了SELECT,但得到了相同的错误.

我为什么会出现这个错误,以及如何修复它? 看起来我误解了GROUP_CONCAT的工作原理.

推荐答案

作为查询的基础,您需要的是discount_product连接表与productsdiscounts表之间的双向联接. 然后,按产品汇总并使用类似的GROUP_CONCAT()逻辑.

SELECT
    p.id,
    p.title,
    p.sale_price,
    GROUP_CONCAT(CONCAT(d.name, ': ', d.min_qty, ': ', d.max_qty, ': ', d.percent)) AS discount_info
FROM products p
LEFT JOIN discount_product dp
    ON dp.product_id = p.id
LEFT JOIN discounts d
    ON d.id = dp.discount_id
WHERE p.has_discount_price = 1 AND p.id = 13
GROUP BY p.id, p.title, p.sale_price;

Mysql相关问答推荐

MySQL 8.0.28覆盖(和函数)索引未使用

SQL - Select 复合主键,条件为其中一个主键

SQL-从一个字段中 Select 值,但另一个字段中不包含零值

MySQL:统计单词在单元格中出现的次数,并将数字放在bra中单词的旁边

如何在JOIN中获取MySQL中的项目总数

为什么嵌套循环逻辑不能按预期工作

根据时间戳分组删除

将类图转换为SQL数据库

按唯一列排序,但保持匹配的列在一起

MySQL 8.0.30 正则表达式词匹配特殊字符

MySQL查询根据同一表中其他字段的值更新表中的字段

减少mysql中的值但不是负数

If else on WHERE 子句

docker-entrypoint-initdb 中的 MySQL 脚本未执行

在连接条件上使用 IS NULL 或 IS NOT NULL - 理论问题

utf8mb4_unicode_ci 与 utf8mb4_bin

在 MySQL 中的子查询上使用 GROUP_CONCAT

在 MySQL 中存储 IPv6 地址

什么是mysql的BETWEEN性能超过..?

mysql GROUP_CONCAT 重复