简体   繁体   English

使用MySQL查询检查字符串中是否存在美元值,但异常

[英]Check if a dollar value exists in a string with exception using mysql query

I am trying to check if an input string contains dollar($) symbol with amount. 我正在尝试检查输入字符串是否包含带金额的dollar($)符号。 If an input contains a dollar with then amount, then the input string is accepted. 如果一个输入包含一个美元,然后一个金额,则输入字符串被接受。 Else, it is not accepted. 否则,不接受。 Also, the input should disregard the $70 . 同样,输入应忽略$ 70

Example inputs : 输入示例:

$str1 = "I have $70 and has $50 change." // Should return true because it has dollar value $50
$str2 = "I have $70 only." //Should return FALSE. $70 is excluded.

This input should be queried from a database which I have in mind something like this : 我应该从这样的数据库中查询此输入:

$sql = "SELECT * FROM {table} where description like '%{$str}%' "

I know how to use like in mysql. 我知道如何使用like在MySQL。 But the problem is how to extract the input so that it can check the input query and disregard the $70. 但是问题是如何提取输入,以便它可以检查输入查询并忽略$ 70。

Does anybody know? 有人知道吗

SELECT * FROM myTable WHERE description LIKE '%$%' AND description NOT LIKE '%$70%'

OR 要么

SELECT * FROM myTable WHERE LOCATE('$',description) > 0 AND LOCATE('$70',description)=0

This one should be a little more specific 这个应该更具体一点

SELECT * FROM myTable WHERE description REGEXP '\$[0-9]+' AND description NOT LIKE '%$70%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM