我有一张桌子
ppv (product_id | property_id | value_id)
和
v (id | property_id | value)
我尝试进行查询以获取products
其中:
select product_id from v, ppv as t INNER JOIN ppv as t2
ON t.product_id = t2.product_id and t1.property_id = t2.property_id
where
( t1.property_id = 1 and v.value > $a and v.value < $b )
and
( t2.property_id = 2 and v.value > $c and v.value < $d )
但是没有任何结果。请帮忙。
所以,
ppv (product_id | property_id | value_id)
1 1 50
1 2 2
1 3 3
2 1 28
2 2 2
2 3 29
下一张表
v( id | property_id | value)
2 2 3
3 3 2
28 1 2600000
29 3 3
50 1 2500000
SELECT `product_id`,
CONVERT(GROUP_CONCAT(v.`property_id`) USING cp1251) as properties
FROM `values` as v INNER JOIN `product_property_values` as ppv
ON ppv.value_id = v.id
WHERE
(v.`property_id` = 1 and v.value >= 2500000 and v.value <= 3000000 )
or
(v.`property_id` = 2 and v.value >= 1 and v.value <= 3 )
GROUP by product_id
HAVING LOCATE('1', properties) and LOCATE('2', properties)
结果:
测试1
v.`property_id` = 1 and v.value >= 2500000 and v.value < 3000000
v.`property_id` = 2 and v.value >= 1 and v.value <= 2
product_id properties
Empty
TEST2
v.`property_id` = 1 and v.value >= 2550000 and v.value < 3000000
v.`property_id` = 2 and v.value >= 1 and v.value <= 4
product_id properties
2 2,1
TEST3
v.`property_id` = 1 and v.value >= 2000000 and v.value < 3000000
v.`property_id` = 2 and v.value >= 1 and v.value <= 4
product_id properties
1 1,2
2 2,1
没关系!
答案 0 :(得分:0)
看起来你实际上并没有加入表v
。另外,我不相信有必要两次链接ppv
。尝试:
SELECT ppv.product_id
FROM v
INNER JOIN ppv ON v.property_id = ppv.property_id
WHERE
(ppv.property_id = 1 and v.value > $a and v.value < $b )
OR
(ppv.property_id = 2 and v.value > $c and v.value < $d )
答案 1 :(得分:0)
第一个问题:什么是t1
?
第二个问题:
t1.property_id = t2.property_id
t1.property_id = 1
t2.property_id = 2
这意味着:1 = 2
。
如果我理解得很好,那么您正在寻找具有以下产品的产品:
property_id = 1
集property_id = 2
集$a
存在,则<{1}}的值property_id = 1
存在,则<{1}}的值$c
存在,则property_id = 2
的值$b
存在,则property_id = 1
的值我认为您正在寻找此请求:
$d
答案 2 :(得分:0)
SELECT `product_id`,
CONVERT(GROUP_CONCAT(v.`property_id`) USING cp1251) as properties
FROM `values` as v INNER JOIN `product_property_values` as ppv
ON ppv.value_id = v.id
WHERE
(v.`property_id` = 1 and v.value >= 2500000 and v.value <= 3000000 )
or
(v.`property_id` = 2 and v.value >= 1 and v.value <= 3 )
GROUP by product_id
HAVING LOCATE('1', properties) and LOCATE('2', properties)
结果:
测试1
v.`property_id` = 1 and v.value >= 2500000 and v.value < 3000000
v.`property_id` = 2 and v.value >= 1 and v.value <= 2
product_id properties
Empty
TEST2
v.`property_id` = 1 and v.value >= 2550000 and v.value < 3000000
v.`property_id` = 2 and v.value >= 1 and v.value <= 4
product_id properties
2 2,1
TEST3
v.`property_id` = 1 and v.value >= 2000000 and v.value < 3000000
v.`property_id` = 2 and v.value >= 1 and v.value <= 4
product_id properties
1 1,2
2 2,1
没关系!