Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MySQL中获取JSON_搜索结果值的合适方法是什么?_Mysql_Sql_Arrays_Json - Fatal编程技术网

在MySQL中获取JSON_搜索结果值的合适方法是什么?

在MySQL中获取JSON_搜索结果值的合适方法是什么?,mysql,sql,arrays,json,Mysql,Sql,Arrays,Json,我有一个JSON数组,如下所示: [{"name": "test@test.com", ...}, {"name": "test2@test.com", ...}, {"name": "test@test.com", ...}, {"name": "blah blah", ...}, {"name": "+70000000", ...}] test@test.com, test2@test.com, test@test.com 我正在尝试获取只包含电子邮件的字符串。如果可能,这必须在sele

我有一个JSON数组,如下所示:

[{"name": "test@test.com", ...}, {"name": "test2@test.com", ...}, {"name": "test@test.com", ...}, {"name": "blah blah", ...}, {"name": "+70000000", ...}]
test@test.com, test2@test.com, test@test.com
我正在尝试获取只包含电子邮件的字符串。如果可能,这必须在select语句中完成

大概是这样的:

[{"name": "test@test.com", ...}, {"name": "test2@test.com", ...}, {"name": "test@test.com", ...}, {"name": "blah blah", ...}, {"name": "+70000000", ...}]
test@test.com, test2@test.com, test@test.com
独特的价值观会更好

我能够将JSON_搜索结果转换为以下文本值:

'$[0].name', '$[1].name', '$[2].name'
使用此代码:

REPLACE(TRIM(TRAILING  ']' from TRIM(LEADING '[' from JSON_SEARCH(accounts, 'all', '%@%'))),'"',"'")
但是我不能用它作为路径参数。获取JSON_搜索结果值的适当方法是什么

以下是我尝试使用的完整代码:

select accounts
  ,JSON_EXTRACT(accounts, REPLACE(TRIM(TRAILING  ']' from TRIM(LEADING '[' from JSON_SEARCH(accounts, 'all', '%@%'))),'"',"'"))
from data
where 1 = 1
  and customer_id =1

使用了MySQL v8。

我会在代码中这样做——就像这样(c#伪代码)

使用Newton.Soft
...
var myList=JsonConvert.Deserialize(jsonStringYouHave);
var emails=myList.Select(x=>x.Email.Distinct();
var emailsInString=emails.Join(',');
哇o)


请注意,在OOP中描述这个算法要简单得多?

我会在代码中这样做(c#伪代码)

使用Newton.Soft
...
var myList=JsonConvert.Deserialize(jsonStringYouHave);
var emails=myList.Select(x=>x.Email.Distinct();
var emailsInString=emails.Join(',');
哇o)


请注意,在OOP?

MySQL 8.0扩展JSON支持中,此算法的描述要简单得多,它具有一个名为
JSON\u TABLE()
的函数,可用于取消JSON数组的测试

以下是一个解决方案,它为数组中出现的每个项目生成一行:

select d.id, t.*
from 
    data d,
    json_table(d.accounts, '$[*]' columns(idx for ordinality, name varchar(50) path '$.name')) t
order by d.id, t.idx
这将产生:

id | idx | name -: | --: | :------------- 1 | 1 | test@test.com 1 | 2 | test2@test.com 1 | 3 | test@test.com 1 | 4 | blah blah 1 | 5 | +70000000 id |名称 -: | :----------------------------------------- 1 | test@test.com,test2@test.com,test@test.com
MySQL 8.0通过一个名为
JSON_TABLE()
的函数扩展了JSON支持,该函数可用于取消JSON数组的测试

以下是一个解决方案,它为数组中出现的每个项目生成一行:

select d.id, t.*
from 
    data d,
    json_table(d.accounts, '$[*]' columns(idx for ordinality, name varchar(50) path '$.name')) t
order by d.id, t.idx
这将产生:

id | idx | name -: | --: | :------------- 1 | 1 | test@test.com 1 | 2 | test2@test.com 1 | 3 | test@test.com 1 | 4 | blah blah 1 | 5 | +70000000 id |名称 -: | :----------------------------------------- 1 | test@test.com,test2@test.com,test@test.com
您想要代码形式的解决方案还是SQL形式的解决方案?我会更容易用Objects来描述解决方案。我可以稍后用python过滤这个JSON,但我想知道是否可以只使用MySQL来完成这个操作。您想要代码中的解决方案还是SQL中的解决方案?我会更容易用Objects来描述这个解决方案。我可以稍后用python过滤这个JSON,但我想知道是否可以只用MySQL来完成这个操作。我猜你想到了我代码的python版本。好。只要工作完成,我想你想到了我代码的Python版本。好。只要工作完成。