我有以下按关系描述的关系表:
Table product(id(PK),name,type(FK),category(FK),brand(FK));
Table Specification (id(PK),name);
Table product_specification_m2m (id(PK),specification(FK),value);
Table speclist (id(PK),specification(FK),product_type(FK),listtype);
对产品运行此查询会带来预期的结果:
select m2m.specification,sl.product_type,sl.listtype
from product_specification_m2m m2m
left join specification s on
s.id = m2m.specification
left join speclist sl on
sl.specification = m2m.specification
where m2m.product=626 and sl.product_type=8 and listtype="short";
+---------------+--------------+----------+
| specification | product_type | listtype |
+---------------+--------------+----------+
| 98 | 8 | short |
| 100 | 8 | short |
+---------------+--------------+----------+
2 rows in set (0.00 sec)
但是对于运行此查询的product_type=8的任何其他产品,都会带来一个空数组!:
select m2m.specification,sl.product_type,sl.listtype
from product_specification_m2m m2m
left join specification s on
s.id = m2m.specification
left join speclist sl on
sl.specification = m2m.specification
where m2m.product=471 and sl.product_type=8 and listtype="short";
0 rows in set (0.00 sec)
For listtype="long" it mysteriously brings :
+---------------+--------------+----------+
| specification | product_type | listtype |
+---------------+--------------+----------+
| 135 | 8 | long |
+---------------+--------------+----------+
1 rows in set (0.00 sec)
谁能指出我的谜团(或者我太笨了,花了3个小时后我也找不到它!)在这里继续!
编辑 :
speclist 表采用一些规范并将它们分类为“短”列表和“长”列表。这就是我在网页上展示它们的方式。候选名单保留 2/3 规格 ID,而长名单通常保留 9/10 规格 ID。
再次强调:
每种产品类型都有一组规格。
每个产品都有该类型规范的子集(产品和规范所属的)
并且每个产品又有两个规格子集,在规格表中按“短”和“长”分类。