我使用sql获取text
值,并将text
字符串转换为整数以比较相同的指定数字。
原谅我可怜的英语。
第1步,我使用这个sql get text
值。(结果是'8000mAh','2000mAh')
SELECT `product_id`, `text`
FROM `jfw_product_attribute`
WHERE `attribute_id` =3
步骤2,我使用sql convert text
string to integer来比较相同的指定数字:(我删除最后三个str'mAh')
SELECT `product_id` ,
CASE
WHEN cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int) <1000
THEN 1
WHEN 1000 <= cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int) <=2000
THEN 2
WHEN 2001 <= cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int) <=3000
THEN 3
WHEN 3001 <= cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int) <=4000
THEN 4
WHEN 4001 <= cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int) <=5000
THEN 5
WHEN 5001 <= cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int) <=8000
THEN 6
WHEN 8001 <= cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int)
THEN 7
END AS `filter_id`
FROM `jfw_product_attribute`
WHERE `attribute_id` =3
我不能正确filter_id
,我只是得到所有filter_id
是2.有人可以帮助我吗?非常感谢你。
答案 0 :(得分:0)
您可以执行以下操作,因为case
语句将与第一个可能的过滤器匹配,因此您需要做的就是根据<=
进行匹配
SELECT `product_id` ,
CASE
WHEN cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int) <1000
THEN 1
WHEN cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int) <=2000
THEN 2
WHEN cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int) <=3000
THEN 3
WHEN cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int) <=4000
THEN 4
WHEN cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int) <=5000
THEN 5
WHEN cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int) <=8000
THEN 6
WHEN 8001 <= cast( REVERSE( SUBSTRING( REVERSE( text ) , 4 ) ) as UNSIGNED int)
THEN 7
END AS `filter_id`
FROM `jfw_product_attribute`
WHERE `attribute_id` =3
答案 1 :(得分:0)
我只是简单地将text
投射到一个数字上;实际上,在数字上下文中使用它将隐式地执行此操作。因此,人们可以这样做:
SELECT product_id, text DIV 1000 + 1 AS filter_id
FROM jfw_product_attribute
WHERE attribute_id = 3
但请注意,这不会使完全产生与原始CASE
声明所预期相同的结果:
它以不同方式处理端点(即等于2000的值将产生3而不是2);以及
它将每千个视为不同的间隔,而最初汇总的是5001-8000和8001+。
那就是说,通过采用这种方法来实现你想要的东西应该是微不足道的。