首页 > 解决方案 > 如何编写我的 sql 查询来拆分列?

问题描述

我在 MySQL 中有一个表


 aaa 1234555 asdasdvad
 asdasdada 4564456 as
 asdadw 8547965 asdasdasd
_________________________

我想通过拆分数字和字母将它拆分为两列,就像

   t1                     t2
_________________   _______________
aaa asdasdvad          1234555
asdasdada as           4564456
asdadw asdasdasd       8547865

我如何编写一个 sql 查询来做到这一点

任何帮助,将不胜感激

标签: mysqlsql

解决方案


我们可以SUBSTRING_INDEX毫不费力地做到这一点:

SELECT
    CONCAT(SUBSTRING_INDEX(col, ' ', 1), ' ', SUBSTRING_INDEX(col, ' ', -1)) AS t1,
    SUBSTRING_INDEX(SUBSTRING_INDEX(col, ' ', 2), ' ', -1) AS t2
FROM yourTable;

在此处输入图像描述

演示

但是,这个答案实际上只是假设您希望将第一项和第三项连接在一起作为一列,并将中间项作为第二列。我不努力检查数字、字母等。


推荐阅读