So I have these 2 tables:
jobs:
-------------------------------------------------
id business_id other_columns
-------------------------------------------------
1 223 xxxxxx
-------------------------------------------------
1 12 xxxxxx
-------------------------------------------------
businesses_ratings:
--------------------------------------------------------------------------------------
id business_id professional communication safety respectful dependability
--------------------------------------------------------------------------------------
1 223 4 2 5 4 3
--------------------------------------------------------------------------------------
2 223 3 5 2 4 5
--------------------------------------------------------------------------------------
3 223 1 2 5 4 4
--------------------------------------------------------------------------------------
I want to select the jobs of a particular business_id, and append to each job the overall rating of that business_id, computed as AVG((AVG(professional), AVG(communication), AVG(safety), AVG(respectful), AVG(dependability))
Can I achieve this in one query?
LE: I'll append here the query I've tried ( containing also the WHERE clause, maybe it'll help explaining better what I need to achieve. Also the thrown error:
SELECT * FROM jobs
CROSS JOIN (
SELECT count(*) totalJobs FROM jobs
WHERE (
( JSON_CONTAINS(skills, '{"id":1,"val":"Carpenter"}') )
AND NOT JSON_CONTAINS(workers, '{"id":6,"fullname":"Cip"}')
AND NOT JSON_CONTAINS(applicants, '{"id":6,"fullname":"Cip"}')
)
) ttl
CROSS JOIN (
SELECT AVG(
(SELECT AVG(professional) FROM businesses_ratings WHERE business_id=jobs.business_id) +
(SELECT AVG(communication) FROM businesses_ratings WHERE business_id=jobs.business_id) +
(SELECT AVG(safety) FROM businesses_ratings WHERE business_id=jobs.business_id) +
(SELECT AVG(respectful) FROM businesses_ratings WHERE business_id=jobs.business_id) +
(SELECT AVG(dependability) FROM businesses_ratings WHERE business_id=jobs.business_id)
) business_rating FROM businesses_ratings WHERE business_id=jobs.business_id
) avg
WHERE (
( JSON_CONTAINS(skills, '{"id":1,"val":"Carpenter"}') )
AND NOT JSON_CONTAINS(workers, '{"id":6,"fullname":"Cip"}')
AND NOT JSON_CONTAINS(applicants, '{"id":6,"fullname":"Cip"}')
)
ORDER BY start_date LIMIT 3
and the error:
Unknown column 'jobs.business_id' in 'where clause'