有人可以在这里帮助我,谷歌用Google搜索,并查看其他示例有这个错误,但它只是让我困惑,我似乎无法理解这一点。
我正在努力选择数据AS mysql查询中的其他东西我得到以下错误...
Operand should contain 1 column(s)
操作数的简化术语是什么?
下面是mysql查询...
SELECT atable_garage.id,
atable_garage.total_votes as num_votes,
atable_garage.name,
atable_garage.address,
atable_garage.city,
atable_garage.postcode,
atable_garage.main_dealer,
atable_garage.phone,
atable_garage.website,
atable_garage.description,
atable_garage.years_in_business,
atable_garage.garage_image,
(SELECT(avg(quality_of_repair) + avg(attitude_of_staff) + avg(overall_satisfaction))/3 as rating,
count(*) as num_votes
FROM atable_feedback
WHERE validated = 'Y'
AND atable_garage.id = garage_id)
FROM `atable_garage`
WHERE valeting = 'Y'
AND active = 'Y'
ORDER BY rating DESC, atable_garage.total_votes DESC
有谁可以指出我在这里做错了什么?如果可能的话,解释一下,我发现很难提前感谢这个问题。
答案 0 :(得分:1)
相关子查询一次只能返回一列。由于您希望拥有两列,因此可以通过将查询转换为LEFT JOIN
来重新调整查询。
SELECT atable_garage.id,
atable_garage.total_votes as num_votes,
atable_garage.name,
atable_garage.address,
atable_garage.city,
atable_garage.postcode,
atable_garage.main_dealer,
atable_garage.phone,
atable_garage.website,
atable_garage.description,
atable_garage.years_in_business,
atable_garage.garage_image,
COALESCE(b.rating, 0) rating,
COALESCE(b.num_votes, 0) num_votes
FROM atable_garage
LEFT JOIN
(
SELECT garage_id,
(avg(quality_of_repair) + avg(attitude_of_staff) + avg(overall_satisfaction)) / 3.0 as rating,
count(*) as num_votes
FROM atable_feedback
WHERE validated = 'Y'
GROUP BY garage_id
) b ON atable_garage.id = b.garage_id
WHERE valeting = 'Y' AND active = 'Y'
ORDER BY rating DESC,
atable_garage.total_votes DESC
答案 1 :(得分:0)
对列使用子选择时,只能返回一个值,如下所示:
(SELECT avg(quality_of_repair) + avg(attitude_of_staff) + avg(overall_satisfaction)
FROM atable_feedback
WHERE validated = 'Y'
AND garage_id = atable_garage.id
)/3 as rating,