搜索 GROUP_CONCAT 以过滤数据
Search GROUP_CONCAT for filtering data
我希望能够搜索我的产品(搜索产品名称或产品 "codes"),然后在这些结果中进行过滤。
我不知道要在 Google 或 SO 中搜索什么,每次出现 Search GROUP_CONCAT using LIKE or Php and MySQL GROUP_CONCAT with separator comma and search where concat by comma,但我似乎无法理解 HAVING
而不会在我的数据库中出现错误。我不确定这是否可能,或者我是否只是路途遥远。
我的table是这样的:
产品Table
+------------+--------------+----------------+-------------------------+---------------+
| product_id | product_name | product_status | product_date_available | product_type |
+------------+--------------+----------------+-------------------------+---------------+
| 1 | Rug | 1 | 2020-01-24 | 0 |
| 2 | Scraper | 1 | 2020-03-15 | 0 |
| 3 | Shovel | 0 | 2019-04-24 | 0 |
| 4 | Bucket | 1 | 2030-01-01 | 0 |
| 5 | Dog | 1 | 2018-04-27 | 0 |
| 6 | Digging | 1 | 2020-01-01 | 1 |
+------------+--------------+----------------+-------------------------+---------------+
产品类别table
+------------+-------------+
| product_id | category_id |
+------------+-------------+
| 1 | 17 |
| 2 | 22 |
| 3 | 22 |
| 4 | 17 |
+------------+-------------+
产品标签Table
+------------+--------+
| product_id | tag_id |
+------------+--------+
| 1 | 11 |
| 2 | 6 |
| 3 | 7 |
| 4 | 7 |
+------------+--------+
产品代码Table
+---------+------------+------------+
| code_id | product_id | code_value |
+---------+------------+------------+
| 1 | 1 | 47012.000 |
| 2 | 1 | 47012.001 |
| 3 | 2 | 47013.000 |
| 4 | 3 | 47014.000 |
| 5 | 3 | 47014.001 |
| 6 | 3 | 47014.002 |
+---------+------------+------------+
产品变体值 Table
+-------+------------+----------+
| pv_id | product_id | value_id |
+-------+------------+----------+
| 1 | 1 | 7 |
| 2 | 1 | 6 |
| 3 | 1 | 10 |
| 4 | 2 | 7 |
| 5 | 3 | 10 |
| 6 | 3 | 7 |
| 7 | 3 | 12 |
| 8 | 2 | 14 |
+-------+------------+----------+
这是我试过但无法开始工作的方法:
搜索产品代码
SELECT p.product_id,
(SELECT GROUP_CONCAT(DISTINCT code_value) FROM `me_product_variant_codes` WHERE `product_id` = p.product_id) AS codes,
(SELECT GROUP_CONCAT(DISTINCT value_id) FROM `me_product_variant_values` WHERE `product_id` = p.product_id) AS variant_values,
(SELECT GROUP_CONCAT(DISTINCT tag_id) FROM `me_product_tags` WHERE `product_id` = p.product_id) AS product_tags
FROM me_product AS p
WHERE (p.product_status = '1' AND p.product_date_available <= CONVERT_TZ(NOW(), 'SYSTEM', '+13:00') AND p.product_type = '0')
AND ((LOWER(p.product_name) LIKE '%47012%')
GROUP BY p.product_id
HAVING codes LIKE '%47012%'
AND HAVING variant_values IN(7, 10)
AND HAVING product_tags IN(11)
ORDER BY product_name DESC
LIMIT 24
我很清楚我可能在这方面有很大的错误,但经过数周的研究,我似乎离找到如何使这项工作有效的答案更近一步。
所以我希望作为一个例子,如果我搜索 47012
它将 return 基于上面的示例数据。
+------------+----------------------+----------------+--------------+
| product_id | codes | variant_values | product_tags |
+------------+----------------------+----------------+--------------+
| 1 | 47012.000, 47012.001 | 7, 6 | 11 |
+------------+----------------------+----------------+--------------+
但我目前遇到此错误
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'GROUP BY p.product_id
HAVING codes LIKE '%47012%'
AND HAVING variant_v' at line 8
即使尝试使用我的查询的简单版本至少得到一些结果也会给我一个错误
简化查询
SELECT p.product_id,
(SELECT GROUP_CONCAT(DISTINCT code_value) FROM `me_product_variant_codes` WHERE `code_product_id` = p.product_id) AS codes
FROM me_product AS p
WHERE (p.product_status = '1' AND p.product_date_available <= CONVERT_TZ(NOW(), 'SYSTEM', '+13:00') AND p.product_type = '0')
AND ((LOWER(p.product_name) LIKE '%47012%')
GROUP BY p.product_id
HAVING codes LIKE '%47012%'
ORDER BY p.product_name DESC
LIMIT 24
给我这个错误
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'GROUP BY p.product_id
HAVING codes LIKE '%47012%'
ORDER BY p.product_n' at line 6
我刚刚注意到的另一个问题是,如果它与 product_name
或 codes
值之一相匹配,我不确定 return 结果如何
您应该摆脱所有那些相关的子查询,而是使用连接到计算各种 CSV 术语的子查询:
SELECT
p.product_id,
COALESCE(vc.codes, 'NA') AS codes,
COALESCE(vv.variant_values, 'NA') AS variant_values,
COALESCE(pt.product_tags, 'NA') AS product_tags
FROM me_product AS p
LEFT JOIN
(
SELECT product_id, GROUP_CONCAT(DISTINCT code_value) AS codes
FROM me_product_variant_codes
WHERE code_value LIKE '%47012%'
GROUP BY product_id
) vc
ON vc.product_id = p.product_id
LEFT JOIN
(
SELECT product_id, GROUP_CONCAT(DISTINCT value_id) AS variant_values
FROM me_product_variant_values
GROUP BY product_id
HAVING COUNT(CASE WHEN value_id = 7 THEN 1 END) > 0 AND
COUNT(CASE WHEN value_id = 10 THEN 1 END) > 0
) vv
ON vv.product_id = p.product_id
LEFT JOIN
(
SELECT product_id, GROUP_CONCAT(DISTINCT tag_id) AS product_tags
FROM me_product_tags
GROUP BY product_id
HAVING COUNT(CASE WHEN tag_id = 11 THEN 1 END) > 0
) pt
ON pt.product_id = p.product_id
WHERE
p.product_status = '1' AND
p.product_date_available <= CONVERT_TZ(NOW(), 'SYSTEM', '+13:00') AND
p.product_type = '0' AND
LOWER(p.product_name) LIKE '%47012%';
我希望能够搜索我的产品(搜索产品名称或产品 "codes"),然后在这些结果中进行过滤。
我不知道要在 Google 或 SO 中搜索什么,每次出现 Search GROUP_CONCAT using LIKE or Php and MySQL GROUP_CONCAT with separator comma and search where concat by comma,但我似乎无法理解 HAVING
而不会在我的数据库中出现错误。我不确定这是否可能,或者我是否只是路途遥远。
我的table是这样的:
产品Table
+------------+--------------+----------------+-------------------------+---------------+
| product_id | product_name | product_status | product_date_available | product_type |
+------------+--------------+----------------+-------------------------+---------------+
| 1 | Rug | 1 | 2020-01-24 | 0 |
| 2 | Scraper | 1 | 2020-03-15 | 0 |
| 3 | Shovel | 0 | 2019-04-24 | 0 |
| 4 | Bucket | 1 | 2030-01-01 | 0 |
| 5 | Dog | 1 | 2018-04-27 | 0 |
| 6 | Digging | 1 | 2020-01-01 | 1 |
+------------+--------------+----------------+-------------------------+---------------+
产品类别table
+------------+-------------+
| product_id | category_id |
+------------+-------------+
| 1 | 17 |
| 2 | 22 |
| 3 | 22 |
| 4 | 17 |
+------------+-------------+
产品标签Table
+------------+--------+
| product_id | tag_id |
+------------+--------+
| 1 | 11 |
| 2 | 6 |
| 3 | 7 |
| 4 | 7 |
+------------+--------+
产品代码Table
+---------+------------+------------+
| code_id | product_id | code_value |
+---------+------------+------------+
| 1 | 1 | 47012.000 |
| 2 | 1 | 47012.001 |
| 3 | 2 | 47013.000 |
| 4 | 3 | 47014.000 |
| 5 | 3 | 47014.001 |
| 6 | 3 | 47014.002 |
+---------+------------+------------+
产品变体值 Table
+-------+------------+----------+
| pv_id | product_id | value_id |
+-------+------------+----------+
| 1 | 1 | 7 |
| 2 | 1 | 6 |
| 3 | 1 | 10 |
| 4 | 2 | 7 |
| 5 | 3 | 10 |
| 6 | 3 | 7 |
| 7 | 3 | 12 |
| 8 | 2 | 14 |
+-------+------------+----------+
这是我试过但无法开始工作的方法:
搜索产品代码
SELECT p.product_id,
(SELECT GROUP_CONCAT(DISTINCT code_value) FROM `me_product_variant_codes` WHERE `product_id` = p.product_id) AS codes,
(SELECT GROUP_CONCAT(DISTINCT value_id) FROM `me_product_variant_values` WHERE `product_id` = p.product_id) AS variant_values,
(SELECT GROUP_CONCAT(DISTINCT tag_id) FROM `me_product_tags` WHERE `product_id` = p.product_id) AS product_tags
FROM me_product AS p
WHERE (p.product_status = '1' AND p.product_date_available <= CONVERT_TZ(NOW(), 'SYSTEM', '+13:00') AND p.product_type = '0')
AND ((LOWER(p.product_name) LIKE '%47012%')
GROUP BY p.product_id
HAVING codes LIKE '%47012%'
AND HAVING variant_values IN(7, 10)
AND HAVING product_tags IN(11)
ORDER BY product_name DESC
LIMIT 24
我很清楚我可能在这方面有很大的错误,但经过数周的研究,我似乎离找到如何使这项工作有效的答案更近一步。
所以我希望作为一个例子,如果我搜索 47012
它将 return 基于上面的示例数据。
+------------+----------------------+----------------+--------------+
| product_id | codes | variant_values | product_tags |
+------------+----------------------+----------------+--------------+
| 1 | 47012.000, 47012.001 | 7, 6 | 11 |
+------------+----------------------+----------------+--------------+
但我目前遇到此错误
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'GROUP BY p.product_id
HAVING codes LIKE '%47012%'
AND HAVING variant_v' at line 8
即使尝试使用我的查询的简单版本至少得到一些结果也会给我一个错误
简化查询
SELECT p.product_id,
(SELECT GROUP_CONCAT(DISTINCT code_value) FROM `me_product_variant_codes` WHERE `code_product_id` = p.product_id) AS codes
FROM me_product AS p
WHERE (p.product_status = '1' AND p.product_date_available <= CONVERT_TZ(NOW(), 'SYSTEM', '+13:00') AND p.product_type = '0')
AND ((LOWER(p.product_name) LIKE '%47012%')
GROUP BY p.product_id
HAVING codes LIKE '%47012%'
ORDER BY p.product_name DESC
LIMIT 24
给我这个错误
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'GROUP BY p.product_id
HAVING codes LIKE '%47012%'
ORDER BY p.product_n' at line 6
我刚刚注意到的另一个问题是,如果它与 product_name
或 codes
值之一相匹配,我不确定 return 结果如何
您应该摆脱所有那些相关的子查询,而是使用连接到计算各种 CSV 术语的子查询:
SELECT
p.product_id,
COALESCE(vc.codes, 'NA') AS codes,
COALESCE(vv.variant_values, 'NA') AS variant_values,
COALESCE(pt.product_tags, 'NA') AS product_tags
FROM me_product AS p
LEFT JOIN
(
SELECT product_id, GROUP_CONCAT(DISTINCT code_value) AS codes
FROM me_product_variant_codes
WHERE code_value LIKE '%47012%'
GROUP BY product_id
) vc
ON vc.product_id = p.product_id
LEFT JOIN
(
SELECT product_id, GROUP_CONCAT(DISTINCT value_id) AS variant_values
FROM me_product_variant_values
GROUP BY product_id
HAVING COUNT(CASE WHEN value_id = 7 THEN 1 END) > 0 AND
COUNT(CASE WHEN value_id = 10 THEN 1 END) > 0
) vv
ON vv.product_id = p.product_id
LEFT JOIN
(
SELECT product_id, GROUP_CONCAT(DISTINCT tag_id) AS product_tags
FROM me_product_tags
GROUP BY product_id
HAVING COUNT(CASE WHEN tag_id = 11 THEN 1 END) > 0
) pt
ON pt.product_id = p.product_id
WHERE
p.product_status = '1' AND
p.product_date_available <= CONVERT_TZ(NOW(), 'SYSTEM', '+13:00') AND
p.product_type = '0' AND
LOWER(p.product_name) LIKE '%47012%';