当前位置:网站首页>How MySQL uses JSON_ Extract() takes JSON value

How MySQL uses JSON_ Extract() takes JSON value

2022-07-05 17:14:00 1024 questions

Catalog

mysql take json The value of a key under the string field

1. Use replace() Replace

2. Use JSON_UNQUOTE()

mysql Handle json character string ,JSON_EXTRACT() Extract content

MySQL since 5.7 After that, I started to support json type

mysql take json The value of a key under the string field

requirement :mysql edition 5.7 And above

SELECT JSON_EXTRACT('{"uid":"asas02234"}', "$.uid");

because json The key value of is in double quotation marks . So you need to remove the double quotation marks .

1. Use replace() Replace select     replace(JSON_EXTRACT(infoJson,'$.uid'),'"','') uidfrom users

laravel Use in JSON_EXTRACT

$applyList = DB::table('invoice')            ->select('applied_at','total_amount','invoice_form','invoice_file','reject_reason')            ->selectRAW('replace(JSON_EXTRACT(invoice_detail,"$.owner_name"),\'"\',\'\') as owner_name')            ->where('uid',Auth::id())->get();2. Use JSON_UNQUOTE()SELECT JSON_UNQUOTE(JSON_EXTRACT('{"id":"3"}', "$.id"));$applyList = DB::table('invoice')            ->select('applied_at','total_amount','invoice_form','invoice_file','reject_reason')            ->selectRAW('JSON_UNQUOTE(JSON_EXTRACT(invoice_detail,"$.owner_name")) as owner_name')            ->where('uid',Auth::id())->get();mysql Handle json character string ,JSON_EXTRACT() Extract content MySQL since 5.7 After that, I started to support json type

The corresponding analytic function is mainly JSON_EXTRACT():

SELECT JSON_EXTRACT('{"ID":"1"}','$.ID');

Execution results :

JSON_EXTRACT('{"ID":"1"}','$.ID')
"1"

Remove quotation marks and use functions JSON_UNQUOTE():

SELECT JSON_UNQUOTE(JSON_EXTRACT('{"ID":"1"}','$.ID'));

Execution results :

JSON_UNQUOTE(JSON_EXTRACT('{"ID":"1"}','$.ID'))
1

The above is personal experience , I hope I can give you a reference , I also hope you can support the software development network .


原网站

版权声明
本文为[1024 questions]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051641540623.html