[英]Extracting numerical values from mySQL column string value
I have a "person" column in a mySQL database that represents the age and weight of a person as a string separated by a comma. 我在mySQL数据库中有一个“人”列,以字符串的形式表示一个人的年龄和体重,用逗号分隔。
Example: "24,175" 示例:“ 24,175”
I want to be able to separate and extract those values and cast them as numbers. 我希望能够分离并提取这些值并将其转换为数字。
Example: turn "24,175" to 示例:将“ 24,175”转到
24 as age 24岁
175 as weight 175重量
So that I can write a query similar to the following 这样我就可以编写类似于以下内容的查询
SELECT person
FROM TABLE
WHERE age>140 OR weight>1000
I want to be able to check for values that are not possible. 我希望能够检查不可能的值。 ie age>140 OR weight >1000.
即年龄> 140或体重> 1000。
I cannot modify the table/environment I'm working with 我无法修改正在使用的表/环境
I only have access to queries. 我只能访问查询。
I'm thinking about solving it this way 我正在考虑这样解决
find the index where the comma exists. 查找逗号所在的索引。 CHARINDEX(',',person)
CHARINDEX(',',person)
Split the string into substrings using LEFT , RIGHT, CAST and CHARINDEX(',',person) 使用LEFT,RIGHT,CAST和CHARINDEX(',',person)将字符串拆分为子字符串
Cast age substring and weight substring to numbers using CAST(age AS INT) CAST(weight AS INT) 使用CAST(age AS INT)将年龄子字符串和weight子字符串转换为数字CAST(weight AS INT)
SELECT person
FROM TABLE
WHERE CAST(LEFT(person,CHARINDEX(',',person) AS INT)>150 OR CAST(RIGHT(person,CHARINDEX(',',person) AS INT) >1000
If I did anything wrong please correct me. 如果我做错了什么,请纠正我。 Are all the functions usable/supported by mySQL?
mySQL是否可以使用/支持所有功能? (RIGHT, LEFT, CHARINDEX) Will this work?
(右,左,CHARINDEX)可以吗?
Exception: Another value for this column could be "unknown". 例外:此列的另一个值可能是“未知”。 Will this cause errors if we're trying to check for the index of , if it doesn't exist in the string?
如果我们尝试检查的索引(如果字符串中不存在),这会导致错误吗? Is there a way to include "unknown" cases in the result and have it output a message of "error, person not recognized"
有没有一种方法可以在结果中包括“未知”案例,并输出“错误,无法识别人员”消息
SELECT person
FROM TABLE
WHERE CAST(LEFT(person,LOCATE(',',person) AS INTEGER)>150 OR CAST(RIGHT(person,(LOCATE(',',person)+1) AS INTEGER) >1000
Instead of Char index use LOCATE im MqSQL 代替Char索引,使用LOCATE im MqSQL
Also note the CAST function 另请注意CAST功能
you can also split is with SUBSTR_INDEX like this: 您还可以使用SUBSTR_INDEX进行拆分,如下所示:
MariaDB [yourschema]> SELECT * FROM spliit;
+----+--------+
| id | d |
+----+--------+
| 1 | 24,175 |
+----+--------+
1 row in set (0.03 sec)
MariaDB [yourschema]> SELECT
-> SUBSTRING_INDEX(d, ',', 1) AS age
-> , SUBSTRING_INDEX(d, ',', -1) AS weight
->
-> FROM spliit;
+------+--------+
| age | weight |
+------+--------+
| 24 | 175 |
+------+--------+
1 row in set (0.00 sec)
MariaDB [yourschema]>
sample 样品
yes, you can direct calculate with it in MySQL 是的,您可以直接在MySQL中使用它进行计算
MariaDB [yourschema]> SELECT
-> SUBSTRING_INDEX(d, ',', 1) + 2 AS age
-> , SUBSTRING_INDEX(d, ',', 1) * 12 AS `month`
-> , SUBSTRING_INDEX(d, ',', -1) + 3 AS weight
-> FROM spliit;
+------+-------+--------+
| age | month | weight |
+------+-------+--------+
| 26 | 288 | 178 |
+------+-------+--------+
1 row in set, 1 warning (0.03 sec)
MariaDB [yourschema]>
You can do this all in the where
clause: 您可以在
where
子句中完成所有操作:
where substring_index(person, ',', 1) + 0 > 140 or
substring_index(person, ',' -1) + 0 > 1000
Note that the + 0
does an silent conversion to integers. 请注意,
+ 0
会进行无声转换为整数。 And, substring_index()
is much more convenient than the functions in SQL Server. 并且,
substring_index()
比SQL Server中的函数方便得多。
You can readily incorporate this logic into a view: 您可以轻松地将此逻辑合并到视图中:
create view v_table as
select t.*,
substring_index(person, ',', 1) + 0 as age,
substring_index(person, ',' -1) + 0 as weight
from table t;
If you want to filter out bad values within the view , you can use a MySQL extension and add: 如果要过滤掉view中的错误值,可以使用MySQL扩展并添加:
having age > 140 or weight > 1000
after the from
clause. 在
from
子句之后。
You also can use VIRTUAL PERSITENT COLUMNS that calculate the fields automatis and you can also use a INDEX on each substr / Integer. 您还可以使用虚拟字段来计算自动字段,也可以在每个substr / Integer上使用INDEX。
sample 样品
MariaDB [yourschema]> CREATE TABLE `splitit` (
-> `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
-> `d` VARCHAR(32) DEFAULT NULL,
-> age INT(11) AS (SUBSTRING_INDEX(d, ',', 1)) PERSISTENT,
-> weight INT(5) AS (SUBSTRING_INDEX(d, ',', -1)) PERSISTENT,
-> PRIMARY KEY (`id`),
-> INDEX idx_age (age),
-> INDEX idx_weight (weight)
-> ) ENGINE=INNODB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.79 sec)
MariaDB [yourschema]> INSERT INTO splitit (d) VALUES ('11,234'),('2,66'),('5,2');
Query OK, 3 rows affected (0.06 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [yourschema]> SELECT * FROM splitit;
+----+--------+------+--------+
| id | d | age | weight |
+----+--------+------+--------+
| 1 | 11,234 | 11 | 234 |
| 2 | 2,66 | 2 | 66 |
| 3 | 5,2 | 5 | 2 |
+----+--------+------+--------+
3 rows in set (0.00 sec)
MariaDB [yourschema]>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.