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();