donkey199024 2015-09-15 12:16
浏览 1327
已采纳

MySQl错误:#1140 - 如果没有GROUP BY子句,混合GROUP列(MIN(),MAX(),COUNT(),...)没有GROUP列是非法的

I have a query to calculate sum of each column from a table selecting sum and column values.

    $query = "select sum(salleryallowance), 
sum(entertainmentexp),
sum(depreciation), 
sum(electricity), 
sum(securitygard),
sum(machinaryrepaire), 
sum(totalrepairing),
sum(othermaintanaice),
sum(postal_charge),
sum(officeexp), 
sum(stationary),
sum(rent_lease_thresher), 
sum(rent_tractor), 
sum(traivlingallowance),
sum(transportaion_cost), 
sum(bank_commition), 
sum(total_exp),
sum(interest_earned), 
bit_farm.name as fname, 
bit_regional.name as rname 
from bit_income_expenditure 
inner join bit_farm on bit_income_expenditure.farm_id = bit_farm.id 
inner join bit_regional on bit_income_expenditure.region_id = bit_regional.id
";
$fetch = mysql_query($query);

$row = mysql_fetch_array($fetch);

// and print my output

echo $row[0]; //and so on....

When I execute this code, it shows following error:

Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause

How can I solve this error?

  • 写回答

2条回答 默认 最新

  • dongpin6941 2015-09-15 12:36
    关注

    If you use any function like sum(),avg(), count()...then by default it will sum/count/avg of all data but if you use any other column based on which you want to categorized your sum/count/avg then you must mention group by after where clause, so your query should be-

    select sum(salleryallowance), 
    sum(entertainmentexp),
    sum(depreciation), 
    sum(electricity), 
    sum(securitygard),
    sum(machinaryrepaire), 
    sum(totalrepairing),
    sum(othermaintanaice),
    sum(postal_charge),
    sum(officeexp), 
    sum(stationary),
    sum(rent_lease_thresher), 
    sum(rent_tractor), 
    sum(traivlingallowance),
    sum(transportaion_cost), 
    sum(bank_commition), 
    sum(total_exp),
    sum(interest_earned), 
    bit_farm.name as fname, 
    bit_regional.name as rname 
    from bit_income_expenditure 
    inner join bit_farm on bit_income_expenditure.farm_id = bit_farm.id 
    inner join bit_regional on bit_income_expenditure.region_id = bit_regional.id
    group by bit_farm.name,bit_regional.name;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入