如何检查字符串 mysql 中的精确整数值

How to check exact integer value in string mysql

我遇到了一个问题,我的列 OFFER_DESCRIPTION 包含带有 Amount(Integer) 的字符串....例如

BUY ABOVE 200

BUY ABOVE 900

BUY ABOVE 300...

用户可以查询类似这样的东西...报价 200...那么我 运行 查询类似这样的东西....

select * from offertable where OFFER_DESCRIPTION like '%200%';

SELECT OFFER_DESCRIPTION FROM offertable WHERE OFFER_DESCRIPTION RLIKE '200';

如果只有 RS.200 报价,它工作正常,但......在其他情况下......有不同的报价集,如......

BUY ABOVE 200

BUY ABOVE 1200 //NEW ONE

BUY ABOVE 2000 //NEW ONE

BUY ABOVE 900

BUY ABOVE 300...

因此,如果执行上述查询....它将 return 3 个结果...

BUY ABOVE 200

BUY ABOVE 1200 //NEW ONE

BUY ABOVE 2000 //NEW ONE

我只想提供确切金额为 200 的报价...

购买 200 以上....我怎样才能做到这一点...请帮忙

如果模式相同,即金额在末尾并用 space 分隔,您可以使用 substring_index 函数

mysql> select substring_index('BUY ABOVE 200',' ',-1) as amount;
+--------+
| amount |
+--------+
| 200    |
+--------+
1 row in set (0.00 sec)

因此您可以按

进行查询
select * from table where 
substring_index(col_name,' ',-1) = '200'

也可以使用rlike进行匹配

mysql> select 'BUY ABOVE 200' rlike '[[:<:]]200[[:>:]]';
+-------------------------------------------+
| 'BUY ABOVE 200' rlike '[[:<:]]200[[:>:]]' |
+-------------------------------------------+
|                                         1 |
+-------------------------------------------+
1 row in set (0.00 sec)

mysql> select 'BUY ABOVE 2000' rlike '[[:<:]]200[[:>:]]';
+--------------------------------------------+
| 'BUY ABOVE 2000' rlike '[[:<:]]200[[:>:]]' |
+--------------------------------------------+
|                                          0 |
+--------------------------------------------+
1 row in set (0.00 sec)

mysql> select 'BUY 300 ABOVE' rlike '[[:<:]]200[[:>:]]';
+-------------------------------------------+
| 'BUY 300 ABOVE' rlike '[[:<:]]200[[:>:]]' |
+-------------------------------------------+
|                                         0 |
+-------------------------------------------+
1 row in set (0.00 sec)

mysql> select 'BUY 200 ABOVE' rlike '[[:<:]]200[[:>:]]';
+-------------------------------------------+
| 'BUY 200 ABOVE' rlike '[[:<:]]200[[:>:]]' |
+-------------------------------------------+
|                                         1 |
+-------------------------------------------+

假设你有数据

  • 购买 200 以上
  • 购买 200 件以上
  • 200多买
  • 购买 2000 以上
  • 购买 300 以上

并且您希望 select 仅匹配 200 ,查询将是

select * from table where
colname rlike '[[:<:]]200[[:>:]]' 

我们可以用更简单的方式做到这一点

SELECT  * 
 FROM TAB
 WHERE  CHARACTER_LENGTH (OREPLACE (STRTOK( word,' ',3)   , '200' ,''))=0