mysql 取json字符串字段下的某個鍵的值
要求:mysql版本5.7及以上
SELECT JSON_EXTRACT('{"uid":"asas02234"}', "$.uid");
由於json的鍵值是帶雙引號。所以需要去掉雙引號。
1.使用replace()做替換
select
replace(JSON_EXTRACT(infoJson,'$.uid'),'"','') uid
from users
laravel 裏使用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.使用 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();