mysql - 如何根据每一行的另一列的值将值插入一列?
问题描述
# table schema
CREATE TABLE `ref_str` (
`ref_id` int(11) DEFAULT NULL,
`ref_str` varchar(4096) DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
这是当前数据:
+--------+--------------------+
| ref_id | ref_str |
+--------+--------------------+
| NULL | [5] www.NCCN.com |
| NULL | [4] www.nhc.gov.cn |
| NULL | [14] www.hc.gov.cn |
| NULL | [146] someh.gov.cn |
+--------+--------------------+
我想迭代地将同一行内的ref_id
int 值插入列中。[]
ref_str
结果应如下所示:
+--------+--------------------+
| ref_id | ref_str |
+--------+--------------------+
| 5 | [5] www.NCCN.com |
| 4 | [4] www.nhc.gov.cn |
| 14 | [14] www.hc.gov.cn |
| 146 | [146] someh.gov.cn |
+--------+--------------------+
我正在使用 MySQL 5.7.32。
谢谢。
解决方案
你可以SUBSTRING_INDEX
在这里使用,例如:
SELECT
SUBSTRING(SUBSTRING_INDEX(ref_str, ']', 1), 2) AS ref_id,
ref_str
FROM ref_str;
演示
推荐阅读
- excel - 使用 Excel 查找包含在两个日期之间的季度
- qt - 移动透明窗口QtQuick时显示错误
- minizinc - 如何通过非标准的 FlatZinc 扩展获得有理数的精确无限精度表示?
- php - 获取数组 OpenStreetMap API Json 失败
- typo3 - TYPO3 FlexForm:如何禁用内联元素中的字段?
- java - 在 Java 重载具有参数的方法时会发生什么,该参数是从另一个方法中获取的参数的子类型?
- azure-devops - Service Fabric 构建中的 PDB
- angular - 在 Cypress 中关闭 Angular Material 对话框
- c# - Azure Maps Batch 地理编码状态 202 12 小时后
- web-config - web.config 文件在多个用户之间共享并防止覆盖