首页 > 解决方案 > 使用mysql Like查询从表中检索十进制值

问题描述

我想从表中检索记录,所有记录都是(宽度 x 高度)格式的十进制值

使用like子句我想获取数据但查询不起作用

Table:

id   widthheight

1     17.14X12.41

2     23.4X39.8


select * from test where widthHeight like '%17X12%'

标签: mysqlsql

解决方案


如上所述,创建两列“宽度”和“高度”

select * from (
select 
    id, 
    SUBSTRING_INDEX(widthHeight, 'X', 1) width,
    SUBSTRING_INDEX(widthHeight, 'X', -1) height
from mytable) x
where floor(x.width)=17 and floor(x.height)=12

您的原始查询不起作用,因为您应该这样做:

select * 
from mytable 
where widthHeight like '%17%' and widthHeight like '%12%'

推荐阅读