我对MySQL json类型有疑问。我想在mysql json数组中搜索并获取行。我的json数据是
{
"id": 361,
"email": "example@outlook.com",
"code": null,
"username": null,
"permissions": null,
"created_at": "2019-03-01 16:09",
"updated_at": "2019-03-01 16:09",
"user_profile": {
"id": 361,
"name": "Jhon",
"surname": "Doe",
"father_name": "NED",
"birthday": "1994-12-15",
"fin_code": "6A56BS7",
"ssn": "AAA12830157",
"account_number": "account123",
"country": "USA",
"city": "NEW YORK",
"address": "EXample r",
"address_n": "Khani/Bina",
"mobile_phone": "(($717865643",
"phone": "0123456789",
"additional_email": "e.example@gmail.com",
"education": [
{
"endDate": "2020-06",
"startDate": "2015-09",
"profession": "Computer Since",
"university": "State University",
"educationType": 99
}
],
"language": [
{
"id": 102,
"level": 106
},
{
"id": 103,
"level": 106
},
{
"id": 104,
"level": 107
}
],
"computer_skills": [
{
"level": 106,
"skill": "php"
},
{
"level": 107,
"skill": "java"
}
],
"family_composition": [
{
"name": "Jhon",
"level": 126,
"surname": "Snow",
"birthday": "1992-02-08",
"fatherName": "Ned"
},
{
"name": "Jhon",
"level": 126,
"surname": "Snow",
"birthday": "1992-05-18",
"fatherName": "Ned"
}
],
"experience": [
{
"job": 128,
"time": 22,
"level": 8,
"salary": 2200,
"jobSign": 128,
"jobStatus": 267,
"startDate": "2012-12-12",
"orderNumber": "123dsa",
"jobSituation": 273,
"additionalSalary": 800
}
],
"reproach": [
{
"doc": 228,
"date": "2011-11-11",
"note": "Some reason",
"level": 225,
"number": "123dsa",
"reason": "islemir",
"article": 233
}
],
"additional_information": "All is work",
"citizen": {
"id": 5,
"name": "United States"
},
"cities": {
"id": 21,
"name": "New York"
},
"countries": {
"id": 89,
"name": "Unated States"
},
"gender": {
"id": 1,
"name": "Man"
},
"marital": {
"id": 4,
"name": "Single"
},
"work": {
"id": 269,
"name": "Able"
},
"party": {
"id": 10,
"name": "Digər"
},
"military": {
"id": 121,
"name": "OK"
},
"institution": null
}
}
我想这样搜索:
WHERE education.'$[*].profession' Like %Computer%
但是此语法不起作用。感谢您的回复。如果有任何建议,我可以在Laravel 5.7中开发我的项目。我不使用JSON_SEARCH()
,因为此函数返回键,但我需要为搜索查询返回行。
答案 0 :(得分:0)
如果您使用的是MySQL 5.7,则应该可以使用
.....WHERE JSON_EXTRACT(education , "$.profession") Like '%Computer%';
答案 1 :(得分:0)
使用MySQL 5.7测试:
SELECT 'found it!' FROM whatever_your_table_name_is
WHERE whatever_your_column_name_is->>'$.user_profile.education[*].profession'
LIKE '%Computer%';
输出,显示WHERE子句与文档匹配:
+-----------+
| found it! |
+-----------+
| found it! |
+-----------+
与MySQL中有关JSON的大多数其他问题一样,我认为最好将数据存储在规范化表中。
答案 2 :(得分:0)
我找到了这个解决方案,对我来说这是工作
UPPER(education->"$[*].profession") LIKE UPPER("% Computer %")
对于Laravel语法,我这样编写PHP代码
$query=$query->whereRaw('UPPER(education->"$[*].profession") LIKE UPPER("%' . $profession . '%")');