gpt4 book ai didi

Use Regex to extract column data in SQL(使用正则表达式提取SQL中的列数据)

转载 作者:bug小助手 更新时间:2023-10-24 21:36:50 32 4
gpt4 key购买 nike



I have a column with JSON data. How can I use regex in SQL to extract the value to columns

我有一个包含JSON数据的专栏。如何在SQL中使用正则表达式将值提取到列


Table json_table

表json_table


Column 1: timestamp
Column 2: name
Column 3: json_column
{
key1 : "value1"
key2 : "value2"
}

I want to do the following

我想做以下几件事


SELECT timestamp, 
name,
(REGEX on json_column) as key1,
(REGEX on json_column) as key2,
FROM json_table

更多回答

Wy not use a JSON function like JSON_EXTRACT for that ?

为什么不使用JSON_EXTRACT这样的JSON函数呢?

Can you, please, add some sample input data, and required output data to your question ? The answers will be more in the direction of YOUR data when you do so...

你能在你的问题中添加一些样本输入数据和所需的输出数据吗?当您这样做时,答案将更多地指向您的数据方向。

优秀答案推荐

Assuming your json_column is actually valid JSON i.e.

假设您的json_Column实际上是有效的JSON,即


{
"key1" : "value1",
"key2" : "value2"
}

You can simply use the inbuilt JSON functionality in MySQL 5.7 and above:

您只需使用MySQL 5.7及更高版本中内置的JSON功能:


SELECT `timestamp`, 
name,
json_column->>'$.key1' AS key1,
json_column->>'$.key2' as key2
FROM `json_table`

Demo on dbfiddle.uk

在dbfiddle.uk上进行演示


If you're not using one of these versions, you really need to upgrade as earlier versions have been out of support for a long time now.

如果你没有使用这些版本中的一个,你真的需要升级,因为以前的版本已经不支持很长时间了。


更多回答

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com