我有两个表,一个是job,第二个是applied_job,我想用一个查询计算结果,请查看这个
job table job_applied table
------------------------- -------------------------------------
j_id| job_title | salary a_id | applied_id | status | job_id
------------------------- -------------------------------------
234 | PHP | 50 2342 | 2 | 1 | 127
235 | Ruby | 102 2362 | 4 | 2 | 127
127 | Python | 150 2322 | 5 | 2 | 127
1289| Java | 180 2326 | 6 | 2 | 127
1274| .net | 180 123 | 8 | 3 | 127
status 1 = selected
status 2 = rejected
status 3 = onhold
现在我希望这样的结果带有一个查询
j_id applied_count rejected_count onhold_count selected
234 0 0 0 0
235 0 0 0 0
127 5 3 1 1
1289 0 0 0 0
1274 0 0 0 0
答案 0 :(得分:2)
您需要的是cat(gsub('("section_id":\\s+)([^,]+),', '\\1{ "$oid" : \\2 },', JSON))
[
{
"section_id": { "$oid" : "58ef93aaa310c97c0c16bcd2" },
"name": "Name1",
"slug": "slug1"
},
{
"section_id": { "$oid" : "58ef93aaa310c97c0c16bcd3" },
"name": "Name2",
"slug": "slug2"
},
{
"section_id": { "$oid" : "58ef93aaa310c97c0c16bcd4" },
"name": "Name3",
"slug": "slug3",
"categories": [
{
"section_id": { "$oid" : "58ef93aaa310c97c0c16bcd5" },
"name": "Name31",
"slug": "slug31"
},
{
"section_id": { "$oid" : "58ef93aaa310c97c0c16bcd6" },
"name": "Name32",
"slug": "slug32",
"categories": [
{
"section_id": { "$oid" : "58ef93aaa310c97c0c16bcd7" },
"name": "Name321",
"slug": "slug321"
},
{
"section_id": { "$oid" : "58ef93aaa310c97c0c16bcd8" },
"name": "Name322",
"slug": "slug322"
},
{
"section_id": { "$oid" : "58ef93aaa310c97c0c16bcd9" },
"name": "Name323",
"slug": "slug323"
}
]
}
]
}
]
,left join
和group by
的组合:
case
结果集中的所有字段都已在select t1.j_id,
count(*) as applied_count
sum(case when t2.status = 2 then 1 end) rejected_count,
sum(case when t2.status = 3 then 1 end) onhold_count,
sum(case when t2.status = 1 then 1 end) selected_count
from job t1
left join
job_applied t2
on t1.j_id = t2.job_id
group by t1.j_id
上可用,但您需要将该表与job_applied
结合使用,因为您希望所有可用的作业,即使没有关联的job_applied行。< / p>