问题如何查找与一列中的值匹配的行(使用=运算符),而另一列应具有子字符串(使用find_in_set或其他子字符串匹配)。
方案:我有三个mysql表:
1.图形:它具有图形的详细信息,例如ID,名称,创建者,创建者,修改者,状态等。为方便起见,我仅提及两列:
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结果:肯定
案例二:搜索红色图形。我的查询:
select figure_id from figure_attribute_mapping
where (attribute_id = 2 AND find_in_set('red',value))
预期答案:1、2、3结果:肯定
情况三:搜索红色正方形。我的查询:
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结果:否定的
情况四:在黄色圆圈图中搜索红色正方形。我的查询:
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结果:否定的
当属性类型相似时,我可以找到fig_id,但是当多个属性出现问题时,则无法找到fig_id。
有人可以帮助创建mysql查询吗?
一种解决方案是对行进行分组,figure_id
并value
通过串联属性来搜索属性。
以下是针对每种情况的查询:
情况一:
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
情况二:
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 |
情况三:
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 |
情况四:
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 |
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句