简体   繁体   English

MySQL的所有行与一列的总和

[英]MySql all rows with sum of one column

I try to create a query to select all rows in a table with sum of one column name newtotal 我尝试创建一个查询以选择表中所有行的总和为一列名newtotal

This is my query 这是我的查询

SELECT * 
FROM   tbl_activities
       ,(SELECT SUM(db_newtotal) as total FROM tbl_activities)  {$sql}

{$sql} is where column_name=input from a seach form {$ sql}是column_name =从搜索形式输入的地方

But I have this error: 但是我有这个错误:

Every derived table must have its own alias 每个派生表必须具有自己的别名

I try to do that: 我尝试这样做:

SELECT * 
FROM tbl_activities
     ,(SELECT SUM(db_newtotal) as total FROM tbl_activities) as Table {$sql} 

But I have this error: 但是我有这个错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Table where db_projectname='Barbara Bui'' at line 1 在第1行的'Table where db_projectname ='Barbara Bui'表附近使用正确的语法,请检查与您的MySQL服务器版本相对应的手册。

I print the query and this is the output 我打印查询,这是输出

SELECT * 
FROM tbl_activities
     ,(SELECT SUM(db_newtotal) as total FROM tbl_activities) as Table 
where db_projectname='Barbara Bui'

The output I want should be like this: ll record with projectname name ='Barbara Bui' with the sum the newtotal 我想要的输出应该是这样的:ll用项目名name ='Barbara Bui'记录的总和为newtotal

projectname   location    Cost
Barbara Bui   verdun      100
Barbara Bui   kaslik      200 
Barbara Bui   achrafieh   500
Total                     800

Query 询问

 SELECT * FROM tbl_activities
        union all
        select db_id,db_category,db_subcategory,db_taskname,db_predecessors,db_unit,db_qty,db_wo,db_duration,db_startdate,db_enddate,db_asd,db_add,db_transferredto,db_prb,db_anotes,db_aduration,db_projectname,db_A,db_AA,db_AAA,db_cost,db_status,db_room,db_floor,db_date,sum(db_newtotal) as total from tbl_activities
       {$sql}

Error is because table is a reserved keyword. 错误是因为table是保留关键字。

You want to use UNION ALL like this: 您要像这样使用UNION ALL

select projectname, location, cost
from tbl_activities {$sql}

union all

select 'Total', null, sum(cost)
from tbl_activities {$sql};

Using the parameter to apply the where clause, it becomes: 使用参数应用where子句,它将变为:

select projectname,
    location,
    cost
from tbl_activities {$sql}

union all

select 'Total',
    null,
    sum(cost)
from tbl_activities {$sql}

If you have more columns in your first part of the UNION ALL, then make sure to all those many nulls in the second select. 如果您在UNION ALL的第一部分中有更多列,请确保在第二个选择中所有这些空值都包含在内。

select db_id, db_category, db_subcategory, db_taskname, db_predecessors, db_unit, db_qty, db_wo, db_duration, db_startdate, db_enddate, db_asd, db_add, db_transferredto, db_prb, db_anotes, db_aduration, db_projectname, db_A, db_AA, db_AAA, db_cost, db_status, db_room, db_floor, db_date, db_newtotal
from tbl_activities {$sql}

union all

select null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, sum(db_newtotal)
from tbl_activities {$sql}

Table is a reserved word in most RDMS so you cannot use it as an alias, try to change to 在大多数RDMS中,表是保留字,因此您不能将其用作别名,请尝试将其更改为

SELECT * FROM tbl_activities,(SELECT SUM(db_newtotal) as total FROM tbl_activities) as myTable {$sql}

and see if that works 看看是否可行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM