[英]how can i select from a mysql table where column value is greater than some value if the column is text and numbers
I have a table called "products" where one column is "SerialNumber". 我有一个名为“产品”的表,其中一列为“ SerialNumber”。 The problem is that this column is text latin1_swedish_ci and values are like "BC00001, BC0002, etc. i need to select all products where serial number is for example greater than some given serial number. i tried selecting like this:
问题在于该列为文本latin1_swedish_ci,其值类似于“ BC00001,BC0002等。我需要选择序列号大于某个给定序列号的所有产品。我尝试选择以下方式:
SELECT * FROM 'products' WHERE 'SerialNumber' >= 'BC00563'
But since this column is text, it' not working. 但是由于此列是文本,因此无法正常工作。 I can't modify the table structure because the this table laready has 20.000 + records.
我无法修改表结构,因为此表已经有20.000条记录。 Any help appreciated!
任何帮助表示赞赏!
Assuming the value is always two characters followed by numbers, you can strip out the characters and convert the remaining values into number
for the comparison, eg: 假设值始终是两个字符后跟数字,则可以去除字符并将剩余的值转换为
number
以进行比较,例如:
SELECT *
FROM `products`
WHERE `SerialNumber` >=
CASE WHEN 'BC00563' regexp '[A-Z]' THEN
CONVERT(SUBSTRING("BC00563", 3, LENGTH("BC00563")), UNSIGNED)
ELSE 'BC00563' END;
You can use the column name instead of constant value. 您可以使用列名代替常量值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.