Mysql多对多关系的多重条件

时间:2012-10-25 07:17:27

标签: mysql sql many-to-many

我有三张桌子,例如:

观看表

id(int)  name(varchar)  ref(int)
1        tissot         1234567
2        addidas        7654321
3        nike           8976543

属性表

id(int)  name(varchar)
1        water_resistant
2        material_body
3        material_bracelet
4        watch_type

property_material表

watch_id  property_id   value
1              1          200
1              2         steel
1              3         leather
1              4         quartz
2              1          50
2              2         plastic
2              3         leather
2              4         quartz
3              1         100
3              2         steel
3              3         rubber
3              4        mechanical

我希望得到所有具有属性的手表,例如:

  • 防水> = 100
  • material_body =钢或塑料
  • material_bracelet =皮革或橡胶
  • watch_type = quartz。

请帮我创建查询。属性可以是大约100。 在理想情况下我想得到结果:

watch_id name    ref   water_res mat_body mat_bracelet watch_type
1        tissot  1234   200       steel    leather      quartz

我试试:

select 
 w.*, pm.value,pm.property_id 
from 
 watch w 
join 
  property_material pm 
 on w.id = pm.watch_id 
where 
  pm.pid in (1,2,3) 
and 
 pm.value in ('100','Steel','Leather');

但是这个查询会返回所有记录,当值为Steel或Leather时,例如,我需要material_body为Steel,而material_bracelet为Leather。

更新:您对此查询的看法是什么?

select 
  name, ref 
from
  watch w
join
(SELECT w.id
  FROM watch w
  JOIN property_material pm on w.id = pm.watch_id
  WHERE (pm.property_id = 1 AND pm.value > 100)
   OR (pm.property_id = 2 AND pm.value IN ('steel','plastic'))
   OR (pm.property_id = 3 AND pm.value IN ('leather','rubber'))
   OR (pm.property_id = 4 AND pm.value = 'quartz')
  Group by w.id
  Having count(*) = 4) as t1
 on t1.id = w.id;

最后更新

select 
  w.name, 
  max(if(pm.property_id='1',pm.value,'')) water_resistant,
  max(if(pm.property_id='2',pm.value,'')) material_body,
  max(if(pm.property_id='3',pm.value,'')) material_bracelet,
  max(if(pm.property_id='4',pm.value,'')) watch_type
from
  watch w
join
(
    SELECT w.id
  FROM watch w
       JOIN property_material pm on w.id = pm.watch_id
  WHERE (pm.property_id = 1 AND pm.value > 100)
      OR (pm.property_id = 2 AND pm.value IN ('steel','plastic'))
      OR (pm.property_id = 3 AND pm.value IN ('leather','rubber'))
      OR (pm.property_id = 4 AND pm.value = 'quartz')
  Group by w.id
  Having count(*) = 4
  ) as t1 on t1.id = w.id
 join property_material pm on w.id = pm.watch_id
GROUP BY w.id

3 个答案:

答案 0 :(得分:2)

这里有几个满足您需求的查询。但是如你所见,他们都是独立的。这就是为什么我在评论中询问你究竟需要什么。

要进行更多测试,请使用您提供的示例数据使用此SQL Fiddle sample code

编辑添加了新查询,以便在一个集合中包含所有条件和所有结果。检查SQL Fiddle sample code here

SELECT w.name,ref,property_id,value 
  FROM watch w
  JOIN property p on w.id = p.id
  JOIN property_material pm on p.id = pm.property_id 
 WHERE (p.id = 1 AND pm.value > 100)
    OR (p.id = 2 AND pm.value IN ('steel','plastic'))
    OR (p.id = 3 AND pm.value IN ('leather','rubber'))
    OR (p.id = 4 AND pm.value = 'quartz');

独立查询:

SELECT w.name Watch_name, p.name Property_name, pm.value Property_value
  FROM watch w
  JOIN property_material pm ON w.id = pm.watch_id
  JOIN property p ON p.id = pm.property_id
 WHERE p.id = 1
   AND pm.value > 100;

WATCH_NAME  PROPERTY_NAME     PROPERTY_VALUE
tissot      water_resistant   200

另一个:

SELECT w.name Watch_name, p.name Property_name, pm.value Property_value
  FROM watch w
  JOIN property_material pm ON w.id = pm.watch_id
  JOIN property p ON p.id = pm.property_id
 WHERE p.id = 2
   AND pm.value IN ('steel','plastic');

WATCH_NAME   PROPERTY_NAME   PROPERTY_VALUE
tissot      material_body      steel
addidas     material_body      plastic
nike        material_body      steel

另一个:

SELECT w.name Watch_name, p.name Property_name, pm.value Property_value
  FROM watch w
  JOIN property_material pm ON w.id = pm.watch_id
  JOIN property p ON p.id = pm.property_id
 WHERE p.id = 3
   AND pm.value IN ('leather','rubber');

WATCH_NAME   PROPERTY_NAME      PROPERTY_VALUE
tissot      material_bracelet      leather
addidas     material_bracelet      leather
nike        material_bracelet      rubber

另一个:

SELECT w.name Watch_name, p.name Property_name, pm.value Property_value
  FROM watch w
  JOIN property_material pm ON w.id = pm.watch_id
  JOIN property p ON p.id = pm.property_id
 WHERE p.id = 4
   AND pm.value = 'quartz';

WATCH_NAME   PROPERTY_NAME   PROPERTY_VALUE
tissot         watch_type       quartz
addidas        watch_type       quartz

答案 1 :(得分:1)

我知道的唯一可能性是为每个属性建立属性表的连接,就像这样(我修改了你给出的例子):

select w.*, pm1.value,pm1.pid ,pm2.value,pm2.pid
    from watch w 
        join property_material pm1 on w.id = pm1.watch_id AND pm1.pid = 1 and pm1.value = '100'
        join property_material pm2 on w.id = pm2.watch_id AND pm2.pid = 2 and pm2.value IN ('Steel','Lather');

答案 2 :(得分:0)

select watch_table.id,watch_table.name,ref,property_id,value from watch_table join        property_table on watch_table.id=property_table.id
  join property_material on property_table.id=property_material.property_id 
 where WHERE property.id = 3 AND property_material.value IN ('leather','rubber');