无效的Group By Function :: MySQL

时间:2011-03-03 00:34:26

标签: mysql group-by sql-like

首先,我已经阅读了匹配名称的帖子,并且我尝试集成了一些解决方案,但我似乎无法让这个SQL查询工作......

问题似乎围绕第8行的COUNT函数。

这是..


SELECT `purchase_orders`.`purchase_order_id`,`purchase_orders`.`sequence_id`,`purchase_orders`.`order_number`,`vendors`.`name`,`purchase_orders`.`date`,COUNT(`purchase_order_items`.`purchase_order_id`) `item_count`,`purchase_orders`.`total_value`,`purchase_orders`.`status`,`users`.`first`
FROM (`purchase_orders`, `vendors`, `purchase_order_items`,`users`) 
WHERE `purchase_orders`.`aid`='c4ca4238a0b923820dcc509a6f75849b'AND`purchase_orders`.`vendor_id`=`vendors`.`vid`AND`purchase_orders`.`created_by`=`users`.`uid`AND`purchase_order_items`.`purchase_order_id`=`purchase_orders`.`purchase_order_id` AND `purchase_orders`.`purchase_order_id` LIKE '%122%' 
OR `purchase_orders`.`aid`='c4ca4238a0b923820dcc509a6f75849b'AND`purchase_orders`.`vendor_id`=`vendors`.`vid`AND`purchase_orders`.`created_by`=`users`.`uid`AND`purchase_order_items`.`purchase_order_id`=`purchase_orders`.`purchase_order_id` AND `purchase_orders`.`sequence_id` LIKE '%122%' 
OR `purchase_orders`.`aid`='c4ca4238a0b923820dcc509a6f75849b'AND`purchase_orders`.`vendor_id`=`vendors`.`vid`AND`purchase_orders`.`created_by`=`users`.`uid`AND`purchase_order_items`.`purchase_order_id`=`purchase_orders`.`purchase_order_id` AND `purchase_orders`.`order_number` LIKE '%122%' 
OR `purchase_orders`.`aid`='c4ca4238a0b923820dcc509a6f75849b'AND`purchase_orders`.`vendor_id`=`vendors`.`vid`AND`purchase_orders`.`created_by`=`users`.`uid`AND`purchase_order_items`.`purchase_order_id`=`purchase_orders`.`purchase_order_id` AND `vendors`.`name` LIKE '%122%' 
OR `purchase_orders`.`aid`='c4ca4238a0b923820dcc509a6f75849b'AND`purchase_orders`.`vendor_id`=`vendors`.`vid`AND`purchase_orders`.`created_by`=`users`.`uid`AND`purchase_order_items`.`purchase_order_id`=`purchase_orders`.`purchase_order_id` AND `purchase_orders`.`date` LIKE '%122%' 
OR `purchase_orders`.`aid`='c4ca4238a0b923820dcc509a6f75849b'AND`purchase_orders`.`vendor_id`=`vendors`.`vid`AND`purchase_orders`.`created_by`=`users`.`uid`AND`purchase_order_items`.`purchase_order_id`=`purchase_orders`.`purchase_order_id` AND COUNT(`purchase_order_items`.`purchase_order_id`) LIKE '%122%'
OR `purchase_orders`.`aid`='c4ca4238a0b923820dcc509a6f75849b'AND`purchase_orders`.`vendor_id`=`vendors`.`vid`AND`purchase_orders`.`created_by`=`users`.`uid`AND`purchase_order_items`.`purchase_order_id`=`purchase_orders`.`purchase_order_id` AND `purchase_orders`.`total_value` LIKE '%122%' 
OR `purchase_orders`.`aid`='c4ca4238a0b923820dcc509a6f75849b'AND`purchase_orders`.`vendor_id`=`vendors`.`vid`AND`purchase_orders`.`created_by`=`users`.`uid`AND`purchase_order_items`.`purchase_order_id`=`purchase_orders`.`purchase_order_id` AND `purchase_orders`.`status` LIKE '%122%' 
OR `purchase_orders`.`aid`='c4ca4238a0b923820dcc509a6f75849b'AND`purchase_orders`.`vendor_id`=`vendors`.`vid`AND`purchase_orders`.`created_by`=`users`.`uid`AND`purchase_order_items`.`purchase_order_id`=`purchase_orders`.`purchase_order_id` AND `users`.`first` LIKE '%122%'  
GROUP BY `purchase_orders`.`purchase_order_id`
ORDER BY `purchase_orders`.`purchase_order_id` ASC 
LIMIT 0,1

3 个答案:

答案 0 :(得分:1)

在SQL中使用COUNT(xxx)时,需要在HAVING子句中使用它:

e.g。

HAVING COUNT(xxx) > yyyy

此外,通常您希望GROUP BY不使用聚合函数的字段。例如:

SELECT person_name
     , SUM(bonuses)
  FROM person x
     , person_bonus y
 WHERE x.person_id = y.person_id

GROUP BY person_name

当我向SELECT语句添加其他列时,我需要对它们使用另一个聚合函数(COUNT,SUM等),或者我需要GROUP BY它们。

同样,您使用聚合函数的任何内容都可以在您的HAVING子句中使用:

    SELECT person_name
         , SUM(bonuses)
      FROM person x
         , person_bonus y
     WHERE x.person_id = y.person_id
   GROUP BY person_name
     HAVING SUM(bonuses) > 50000  // I wish :)

答案 1 :(得分:1)

虽然您可能选择了问题的解决方案,但是您的查询确实是不干净的。 SQL的前提是确定关于表如何与给定键相关并在where中应用任何EXTRA过滤器的关系。您的查询多次出现“或”。这是一个更清理的查询版本。注意where子句有你正在寻找的ONE键“aid”,还有一个用于连接表的AND子句......在查询下是剩下的AND子句,你有这么多元素有LIKE'%122%'哪个一个COUNT(任何东西)都会返回一个数字。

SELECT 
       po.purchase_order_id,
       po.sequence_id,
       po.order_number,
       v.name,
       po.date,
       COUNT( poi.purchase_order_id) item_count,
       po.total_value,
       po.status,
       users.first
    FROM 
       purchase_orders po,
       vendors v,
       purchase_order_items poi,
       users
    WHERE 
           po.aid = 'c4ca4238a0b923820dcc509a6f75849b'
       AND po.vendor_id = v.vid
       AND po.purchase_order_id = poi.purchase_order_id
       AND po.created_by = users.uid
   GROUP BY 
       po.purchase_order_id
   ORDER BY 
       po.purchase_order_id
   LIMIT 
       0, 1

这些我不明白你在寻找什么,但最终会在我上面的所有其他WHERE条款之后......再次,COUNT(poi.purchase_order_id)喜欢'%122%'我不喜欢认为是适用的。可能我认为其他基于“ID”的列是基于数字的。

       AND po.purchase_order_id LIKE '%122%' 
       AND po.sequence_id LIKE '%122%' 
       AND po.order_number LIKE '%122%' 
       AND v.name LIKE '%122%' 
       AND po.date LIKE '%122%' 
       AND COUNT(poi.purchase_order_id) LIKE '%122%'
       AND po.total_value LIKE '%122%' 
       AND po.status LIKE '%122%' 
       AND users.first LIKE '%122%'  

您似乎有意为每个实例应用所有连接条件,但应该是 - 为了澄清

where 
     ( all first group of conditions )
  OR ( all next group of conditions )
  OR ( all next, etc )

我希望这有助于澄清查询的结构。

答案 2 :(得分:0)

使用GROUP BY子句的规则:

  1. GROUP BY的列也必须在SELECT语句中。
  2. 请记住按要提供信息的列进行分组,而不是要应用聚合函数的列。
  3. 在您的示例中,您正在检查purchase_order_items.purchase_order_id = purchase_orders.purchase_order_id,并且您正在应用聚合函数COUNT(purchase_order_items.purchase_order_id)