我还有一个名为“resource_ratings”的表,它将resource_id作为外键,并将列命名为“rating_level”。我需要获得rating_level列的AVG。但我想我做错了什么。所以我复制了工作查询而没有得到AVG,所以你可以从工作查询开始并获得额外的数据。
SELECT
a.company_id,
a.company_code,
a.company_name,
a.company_name_s,
b.resource_id,
b.resource_code,
b.resource_name,
b.resource_name_s,
b.spec_id,
b.resource_description,
b.resource_description_s,
d.slot_id,
d.slot_period_minutes,
d.slot_desc,
e.res_slot,
e.from_time,
e.to_time,
e.slot_date
FROM
companies a,
resources b,
resource_periods c,
period_slots d,
resource_slots e
WHERE
a.company_id = b.company_id
AND a.is_active = 1
AND b.is_active = 1
AND b.company_id = c.company_id
AND b.resource_id = c.resource_id
AND c.allow_publish = 1
AND c.company_id = d.company_id
AND c.resource_id = d.resource_id
AND c.period_id = d.period_id
AND d.is_active = 1
AND d.company_id = e.company_id
AND d.resource_id = e.resource_id
AND d.period_id = e.period_id
AND d.slot_id = e.slot_id
AND e.slot_status = 1
AND a.company_id = IFNULL(@company_id, a.company_id)
AND a.country_id = @country_id
AND a.city_id = @city_id
AND b.resource_id IN (SELECT
f.resource_id
FROM
resource_specialities f
WHERE
b.resource_id = f.resource_id
AND f.spec_id = @speciality_id)
AND e.slot_date = @slot_date
答案 0 :(得分:0)
加入获得平均值的子查询。您需要更改交叉产品语法以显示JOIN
,因此您可以在此处使用LEFT JOIN
(如果子查询中没有匹配的行)。
SELECT
a.company_id,
a.company_code,
a.company_name,
a.company_name_s,
b.resource_id,
b.resource_code,
b.resource_name,
b.resource_name_s,
b.spec_id,
b.resource_description,
b.resource_description_s,
d.slot_id,
d.slot_period_minutes,
d.slot_desc,
e.res_slot,
e.from_time,
e.to_time,
e.slot_date,
f.avg_rating
FROM companies a
JOIN resources b ON a.company_id = b.company_id
JOIN resource_periods c
ON b.company_id = c.company_id
AND b.resource_id = c.resource_id
JOIN period_slots d
ON c.company_id = d.company_id
AND c.resource_id = d.resource_id
AND c.period_id = d.period_id
JOIN resource_slots e
ON d.company_id = e.company_id
AND d.resource_id = e.resource_id
AND d.period_id = e.period_id
AND d.slot_id = e.slot_id
LEFT JOIN (
SELECT resource_id, AVG(rating_level) AS avg_rating
FROM resource_ratings
GROUP BY resource_id) AS f
ON f.resource_id = b.resource_id
WHERE
a.is_active = 1
AND b.is_active = 1
AND c.allow_publish = 1
AND d.is_active = 1
AND e.slot_status = 1
AND a.company_id = IFNULL(@company_id, a.company_id)
AND a.country_id = @country_id
AND a.city_id = @city_id
AND b.resource_id IN (SELECT
f.resource_id
FROM
resource_specialities f
WHERE
b.resource_id = f.resource_id
AND f.spec_id = @speciality_id)
AND e.slot_date = @slot_date
答案 1 :(得分:0)
SELECT
a.company_id,
a.company_code,
a.company_name,
a.company_name_s,
b.resource_id,
b.resource_code,
b.resource_name,
b.resource_name_s,
b.spec_id,
b.resource_description,
b.resource_description_s,
(select avg(rating_level)
from resource_ratings
where resource_id = b.resource_id ) as avg_rating_level,
d.slot_id,
d.slot_period_minutes,
d.slot_desc,
e.res_slot,
e.from_time,
e.to_time,
e.slot_date
FROM
companies a,
resources b,
resource_periods c,
period_slots d,
resource_slots e,
resource_specialities f
WHERE
a.company_id = b.company_id
AND a.is_active = 1
AND b.is_active = 1
AND b.company_id = c.company_id
AND b.resource_id = c.resource_id
AND c.allow_publish = 1
AND c.company_id = d.company_id
AND c.resource_id = d.resource_id
AND c.period_id = d.period_id
AND d.is_active = 1
AND d.company_id = e.company_id
AND d.resource_id = e.resource_id
AND d.period_id = e.period_id
AND d.slot_id = e.slot_id
AND e.slot_status = 1
AND a.company_id = IFNULL(@company_id, a.company_id)
AND a.country_id = @country_id
AND a.city_id = @city_id
AND b.resource_id = f.resource_id
AND f.spec_id = @speciality_id
AND e.slot_date = @slot_date