如何在 MySQL 中使用 JSON_EXTRACT 并获取不带引号的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37403039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use JSON_EXTRACT in MySQL and get a string without the quotes?
提问by Alex
If I have a simple SELECT statement like this:
如果我有一个像这样的简单 SELECT 语句:
SELECT JSON_EXTRACT('{"username":"Alexander"}', '$.username');
I would expect it to return Alexander
, but instead it returns "Alexander"
.
How can I get rid of the quotes? Why does this function even return the quotes too?
我希望它返回Alexander
,但它返回"Alexander"
。我怎样才能摆脱引号?为什么这个函数也返回引号?
回答by Raul
You can use JSON_UNQUOTE
to achieve this.
您可以使用它JSON_UNQUOTE
来实现这一点。
select JSON_UNQUOTE(JSON_EXTRACT(base, '$.scope')) as scope from t_name
ref: Functions That Modify JSON Values
参考:修改 JSON 值的函数
回答by Tharindu Kalhara
You can use SUBSTRING
您可以使用子字符串
SELECT SUBSTRING( JSON_EXTRACT ( '{"username":"Alexander"}', '$.username' ), 2, ( LENGTH( JSON_EXTRACT ( '{"username":"Alexander"}', '$.username' ) ) - 2 ) );
回答by ombre04
you can use replace() with it to remove quotation marks
您可以使用 replace() 来删除引号
SELECT replace(JSON_EXTRACT('{"username":"Alexander"}', '$.username'), '\"', '');