![](/img/trans.png)
[英]How can I subtract a user input value from a value in a MySQL table only if the value in the field is equal to or greater than the input value?
[英]how can i select from a mysql table where column value is greater than some value if the column is text and numbers
我有一个名为“产品”的表,其中一列为“ SerialNumber”。 问题在于该列为文本latin1_swedish_ci,其值类似于“ BC00001,BC0002等。我需要选择序列号大于某个给定序列号的所有产品。我尝试选择以下方式:
SELECT * FROM 'products' WHERE 'SerialNumber' >= 'BC00563'
但是由于此列是文本,因此无法正常工作。 我无法修改表结构,因为此表已经有20.000条记录。 任何帮助表示赞赏!
假设值始终是两个字符后跟数字,则可以去除字符并将剩余的值转换为number
以进行比较,例如:
SELECT *
FROM `products`
WHERE `SerialNumber` >=
CASE WHEN 'BC00563' regexp '[A-Z]' THEN
CONVERT(SUBSTRING("BC00563", 3, LENGTH("BC00563")), UNSIGNED)
ELSE 'BC00563' END;
您可以使用列名代替常量值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.