mysql - 使用逗号作为分隔符将列拆分为 2 个 clomun
问题描述
我有桌子listings
:
location (Primary Key)
Chicago, USA
New York, USA
Paris, France
格式严格
City, Country
如同:
<City><comma><single_space><Country>
我想要 2 列location
(使用逗号作为分隔符)作为:
city country
Chicago USA
New York USA
Paris France
解决方案
在 MySQL 中,您可以使用substring_index()
:
select
location,
substring_index(location, ', ', 1) city,
substring_index(location, ', ', -1) country
from listings
位置 | 城市| 国家 :------------ | :------- | :------ 美国芝加哥 | 芝加哥 | 美国 美国纽约 | 纽约 | 美国 法国巴黎 | 巴黎 | 法国
如果你想要一个update
声明:
update listings
set
city = substring_index(location, ', ', 1),
country = substring_index(location, ', ', -1)
推荐阅读
- c# - 如何在不冻结应用程序的情况下从 URL 加载 C# Winforms 中的图像?
- google-apps-script - 如何在谷歌应用脚本项目中删除另一个人的触发器?
- docker - K6 InfluxDB + Grafana Docker:实际需要哪些文件夹和文件,哪些可以删除?
- react-native - 如何正确弯曲 Safeview?
- c++ - 在这本 cpp 书中解释这条 c++ bool 行的含义
- dependency-injection - 在类型“System.Collections.Generic.List`1[Portal.Models.FileDataModel]”中找到了接受所有给定参数类型的多个构造函数
- python-3.x - 某些 AWS lambda 函数因“没有名为 setuptools._distutils 的模块”错误而停止工作
- opengl - GL_CURRENT_VERTEX_ATTRIB - 文档说明
- java - 将 Jython 与 Java 一起使用时出现 ImportError
- reactjs - 如何从钩子反应组件中获取 TextField(Material-UI) 值?