gpt4 book ai didi

sql - 删除 JSON_AGG(聚合函数)中的括号和引号

转载 作者:行者123 更新时间:2023-12-04 11:34:46 25 4
gpt4 key购买 nike

public function fetchdrug(Request $search_drug){

$filter_drug = $search_drug->input('search_drug');
$all_drugs = HmsBbrKnowledgebaseDrug::selectRaw('DISTINCT ON (drug_code)
drug_code,
drug_name,
JSON_AGG(drug_dosage) AS dosage_list')
->GroupBy('drug_code', 'drug_name')
->orderBy('drug_code', 'ASC')
->get();

return response()->json([
'all_drugs'=>$all_drugs,
]);
}
我正在使用 JSON_AGG检索 drug_dosage 的多行并将它们合二为一,但我的输出中出现了括号和引号,我该如何将其取出?
1
更新:
我在示例中遇到错误,因为我正在尝试使用 str_replace 的解决方案和 preg_replace .我的问题是目标在 SQL 语句中,所以我怀疑这与错误有关,因为结果中还有其他数据
错误:
  Uncaught TypeError: Cannot use 'in' operator to search for 'length' in 
{"drug_code":"CFZU",
"drug_name":"Cefazolin",
"dosage_list":"[\"<=4 mg\/L\", \"<=3 mg\/L\"]"},
{"drug_code":"TZPD","drug_name":"Pip\/Tazobactam",
"dosage_list":"[\"Pip\/Tazobactam\"]"}

最佳答案

你可以试试string_agg相反 JSON_AGG

public function fetchdrug(Request $search_drug){

$filter_drug = $search_drug->input('search_drug');
$all_drugs = HmsBbrKnowledgebaseDrug::selectRaw('DISTINCT ON (drug_code)
drug_code,
drug_name,
string_agg(drug_dosage, ', ') AS dosage_list')
->GroupBy('drug_code', 'drug_name')
->orderBy('drug_code', 'ASC')
->get();

return response()->json([
'all_drugs'=>$all_drugs,
]);
}

关于sql - 删除 JSON_AGG(聚合函数)中的括号和引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68916011/

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