一个神秘的MySQL选择查询
我将以下关系表按关系描述为:一个神秘的MySQL选择查询,mysql,activerecord,codeigniter-2,mysql-5.5,Mysql,Activerecord,Codeigniter 2,Mysql 5.5,我将以下关系表按关系描述为: 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); 对
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);
对产品运行此查询会产生预期的结果:
但是对于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)
有人能告诉我这个秘密吗?或者说我太笨了,花了三个小时也找不到它!这是怎么回事
编辑:
speclist表接受一些规范,并将它们分类为短列表和长列表。这就是我在网页上显示它们的方式。入围名单保留2/3的等级库ID,long通常保留9/10的等级库ID
再次点燃东西:
每种产品类型都有一套规格
每个产品都有产品和规范所属类型的规范子集
在speclist表中,每个产品都有两个子集的规格,按短和长分类 从WHERE子句中排除m2m.product=471,并检查该条件下存在哪些产品:sl.product_type=8和LISTYPE=short
您为listtype=long编写了它,它神秘地带来了一条记录。这并不神秘。您查找产品471,得到的是listtype=long,那么为什么要得到listtype=short的产品呢?我假设它不存在于这个列表类型中请提供列类型并检查长字段中是否有空格。要测试,请尝试使用listtype,如%short%@edam发布一个不带m2m的查询输出。product=471条件。它只带来了属于sl.product_type=X的整套规范@是的,当然。现在检查该集合中是否有product=471。
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)