0

有人可以在这里帮助我吗,我用谷歌搜索过,并查看了其他有此错误的示例,但这只是让我感到困惑,我似乎无法理解这一点。

我正在努力在 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

谁能指出我在这里做错了什么?如果可能的话,请提前解释一下,我发现很难理解这一点。

4

2 回答 2

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
于 2013-04-06T03:53:58.893 回答
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,
于 2013-04-06T03:21:55.580 回答