从表中获取与一列中的值匹配的行以及在其他列中以逗号分隔的字符串存在子字符串

时间:2017-01-13 10:04:52

标签: mysql substring find-in-set

问题如何在一列中找到与值匹配的行(使用=运算符),其他列应该有子字符串(使用find_in_set或其他子字符串匹配)。

场景:我有三个mysql表:

1。数字:它有详细的数字,如id,name,create-by,created-on,modified-on,status等,以方便我只提到两列:

 id |  name 
 1  | red green yellow circle
 2  | red square in yellow circle
 3  | 3D yellow red trapezium

2。属性:它存储不同的属性以及每个属性的所有可能的逗号分隔值。

id  |  term     | value
 1  |  shape    | circle,square,rectangle,parallelogram,trapezium
 2  |  color    | red,green,yellow,blue,white,orange
 3  | dimension | 1D,2D,3D

第3。图属性映射:它存储图形,属性的映射,并且只存储适用于该特定图形属性组合的逗号分隔值。

id | figure_id | attribute_id | value
 1 | 1         | 1            | circle
 2 | 1         | 2            | red,green,yellow
 3 | 2         | 1            | circle,square
 4 | 2         | 2            | red,yellow
 5 | 3         | 1            | trapezium
 6 | 3         | 2            | yellow,red
 7 | 3         | 3            | 3D

目标:我想编写一个查询,从 figure_attribute_mapping 表中返回 figure_id 对该属性 - 图形映射行中的匹配属性值

案例I :搜索方形图。 我的查询:

Select figure_id from figure_attribute_mapping 
where (attribute_id = 1 AND find_in_set('square',value)<>0);

预期答案:2 结果:正面

案例II :搜索红色图。 我的查询:

select figure_id from figure_attribute_mapping 
where (attribute_id = 2 AND find_in_set('red',value))

预期答案:1,2,3 结果:正面

案例III :搜索红色方形图。 我的查询:

select figure_id from figure_attribute_mapping 
where 
      (attribute_id = 1 AND find_in_set('square',value)<>0) 
  AND (attribute_id = 2 AND find_in_set('red',value)<>0)

预期答案:2 结果:否定

案例IV :在黄色圆圈图中搜索红色正方形。 我的查询:

 select figure_id from figure_attribute_mapping 
where
  ( 
       (attribute_id = 1 AND find_in_set('square',value)<>0) 
  AND  (attribute_id = 1 AND find_in_set('circle',value)<>0)
  ) 
AND
 ( 
       (attribute_id = 2 AND find_in_set('red',value) <>0) 
  AND (attribute_id = 2 AND find_in_set('yellow',value)<>0) 
 )

预期答案:2 结果:否定

我可以在属性类型相似时找到figure_id,但在多个属性出现问题时找不到figure_id。

有人可以帮忙创建一个mysql查询。

1 个答案:

答案 0 :(得分:1)

一种解决方案是按figure_id对行进行分组,并通过连接来搜索属性value

以下是每个案例的查询:

案例I:

SELECT z.* FROM (
    SELECT id, figure_id, GROUP_CONCAT(value) AS merged_value
    FROM figure_attribute_mapping
    GROUP BY figure_id
) z
WHERE FIND_IN_SET('square', z.merged_value);
  

输出

id     | figure_id | merged_value
+------+-----------+--------------------------+
|    3 |         2 | circle,square,red,yellow

案例II:

SELECT z.* FROM (
    SELECT id, figure_id, GROUP_CONCAT(value) AS merged_value
    FROM figure_attribute_mapping
    GROUP BY figure_id
) z
WHERE FIND_IN_SET('red', z.merged_value);
  

输出

| id   | figure_id | merged_value             |
+------+-----------+--------------------------+
|    1 |         1 | circle,red,green,yellow  |
|    3 |         2 | circle,square,red,yellow |
|    5 |         3 | trapezium,yellow,red,3D  |

案例III:

SELECT z.* FROM (
    SELECT id, figure_id, GROUP_CONCAT(value) AS merged_value
    FROM figure_attribute_mapping
    GROUP BY figure_id
) z
WHERE FIND_IN_SET('square', z.merged_value)
AND FIND_IN_SET('red', z.merged_value);

| id   | figure_id | merged_value             |
+------+-----------+--------------------------+
|    3 |         2 | circle,square,red,yellow |

案例IV:

SELECT z.* FROM (
    SELECT id, figure_id, GROUP_CONCAT(value) AS merged_value
    FROM figure_attribute_mapping
    GROUP BY figure_id
) z
WHERE FIND_IN_SET('square', z.merged_value)
AND FIND_IN_SET('circle', z.merged_value)
AND FIND_IN_SET('red', z.merged_value)
AND FIND_IN_SET('yellow', z.merged_value);
  

输出

| id   | figure_id | merged_value             |
+------+-----------+--------------------------+
|    3 |         2 | circle,square,red,yellow |