我有一个名为D1_VAL
此列中的行具有如下所示的数据:
C:10
R:200
E:3
N:77
我正在尝试使用逻辑运算符对这些值进行搜索。我试过这个:
SELECT * FROM d1 WHERE CAST(`D1_VAL` AS UNSIGNED) > 5
它不返回任何行。我也试过:
SELECT * FROM d1 WHERE (0 + `D1_VAL`) > 5
我将如何正确地做到这一点?
这个查询会做,
SELECT * FROM d1 WHERE CAST(SUBSTRING(`D1_VAL`, 3) AS UNSIGNED) > 5
但最好将值标准化C:10
为两个单独的列。
create table d1(
letter char(1),
number int,
// ... other columns
)
您要进行的比较是什么?
如果将数字(在冒号右侧)与 5 进行比较,请尝试以下操作:
CAST(SUBSTRING(`D1_VAL`, INSTR(`D1_VAL`, `:`)) AS UNSIGNED) > 5
有关字符串函数的更多信息,请参见此处。
Also, I would suggest having a think about the database design - i.e. would the contents of this column be better off being stored in two columns - e.g. a char and an integer? That would be more efficient - requiring less disk space and allowing faster queries. If you have lots of rows, doing various string functions, casting etc. could make your queries run quite slowly.