多条件多表的MySQL查询
我有三张表:产品、属性和产品属性。表结构和值的子集如下所示:多条件多表的MySQL查询,mysql,sql,multiple-tables,multiple-conditions,Mysql,Sql,Multiple Tables,Multiple Conditions,我有三张表:产品、属性和产品属性。表结构和值的子集如下所示: products --------------------------------------------- | id | name | description | price | --------------------------------------------- | 1 | Camera | Color Camera | 100 | | 2 | Lens 1 | Camera
products
---------------------------------------------
| id | name | description | price |
---------------------------------------------
| 1 | Camera | Color Camera | 100 |
| 2 | Lens 1 | Camera Lens 1 | 20 |
| 3 | Lens 2 | Camera Lens 2 | 30 |
---------------------------------------------
properties
------------------------------------------
| id | name | display |
------------------------------------------
| 1 | lens_mount | Lens Mount |
| 2 | image_circle | Image Circle |
| 3 | focal_length | Focal Length |
| 4 | lens_family | Lens Family |
------------------------------------------
product_properties
------------------------------------------------
| id | value | product_id | property_id |
------------------------------------------------
| 1 | F-Mount | 2 | 1 |
| 2 | C-Mount | 3 | 1 |
| 3 | 42.01 mm | 2 | 2 |
| 4 | 13.00 mm | 3 | 2 |
| 5 | 10.00 | 2 | 3 |
| 6 | 12.00 | 3 | 3 |
| 7 | Standard | 1 | 4 |
| 8 | Standard | 2 | 4 |
| 9 | Standard | 3 | 4 |
------------------------------------------------
这是我的输出条件:
找到相机1的所有镜头(按镜头系列匹配相机和镜头,这里是“标准”镜头),镜头安装为“F-mount”,图像圆圈>=40 mm,焦距>5
我尝试了以下查询:
SELECT * FROM products
INNER JOIN product_properties ON products.id = product_properties.product_id
INNER JOIN properties ON properties.id = product_properties.property_id
WHERE (product_properties.value = 'Standard')
AND (properties.name = 'lens_mount' AND product_properties.value = 'F-Mount')
AND (properties.name = 'image_circle' AND product_properties.value >= ABS('40 mm'))
AND (properties.name = 'focal_length' AND product_properties.value >= 5)
但是,只有当只有一个条件时,此查询才会给出正确的结果。在所有的条件下,它不会给出任何值。我尝试使用where条件或在where条件下替换和,但这也无助于获得正确的输出
有人能告诉我这个吗。提前感谢。您希望在
having
子句中执行逻辑,而不是在where
子句中执行逻辑:
SELECT products.id
FROM products
INNER JOIN product_properties ON products.id = product_properties.product_id
INNER JOIN properties ON properties.id = product_properties.property_id
group by products.id
having sum(properties.name = 'lens_family' AND product_properties.value = 'Standard') > 0 and
sum(properties.name = 'lens_mount' AND product_properties.value = 'F-Mount') > 0 and
sum(properties.name = 'image_circle' AND product_properties.value >= ABS('40 mm')) > 0 and
sum(properties.name = 'focal_length' AND product_properties.value >= 5) > 0;
您正在寻找单个产品的一组属性。没有一行可以匹配所有条件——它们相互冲突。相反,使用GROUPBY
子句将给定产品的行组合在一起。然后计算与每个条件匹配的行数
having
中的每个子句对应于where
语句中的一个原始子句,该语句由sum()括起。这将统计匹配的行数。这些条件确保每个属性至少有一行。啊,可怕的EVA模式。
您应该做的第一件事是创建一个视图,按照下面的SQL规范化数据
然后,您可以使用该视图的两个副本(一个用于相机,一个用于镜头),并将它们连接在一起以获得所需的内容
SELECT p.id
,p.name
,pp1.value as Lens_Mount
,pp2.value as Image_Circle
,pp3.value as Focal_Length
,pp4.value as Lens_Family
from products p
left outer join
product_properties pp1
on pp1.product_id = p.id
and pp1.property_id = 1 --lens Mount
left outer join
product_properties pp2
on pp2.product_id = p.id
and pp2.property_id = 2 --Image Circle
left outer join
product_properties pp3
on pp3.product_id = p.id
and pp3.property_id = 3 --Focal Length
left outer join
product_properties pp4
on pp4.product_id = p.id
and pp4.property_id = 3 --Lens Family
where p.name like 'Lens%'
您应该了解每个条件,并计算每个产品id满足的条件数量。
因此,您可以按产品id对结果进行分组,并使用HAVING
子句过滤结果
SELECT product_properties.product_id, count(*) as conditions_satisfied FROM products
INNER JOIN product_properties ON products.id = product_properties.product_id
INNER JOIN properties ON properties.id = product_properties.property_id
WHERE product_properties.value = 'Standard'
OR properties.name = 'lens_mount' AND product_properties.value = 'F-Mount'
OR properties.name = 'image_circle' AND product_properties.value >= ABS('40 mm')
OR properties.name = 'focal_length' AND product_properties.value >= 5
GROUP BY product_properties.product_id
HAVING COUNT(*) = 4
注意:如果确实需要数字比较,则比较字符串属性值可能会比较困难。我喜欢这种解决方案。您可能应该首先添加
properties.name='lens\u family'和
到具有子句标准(product\u properties.value='Standard'
)的中,以防存在可能将“Standard”作为值的不同属性。