简体   繁体   English

如何从两个表(如一个表的所有行)以及相对于其他表的第一个表的主键总和中获取数据

[英]How to get data from two tables like one table all rows and with respect to first table primary key sum of row from other table

Table 1: 表格1:

Table 2: 表2:

Result table: 结果表:

I am in the need of efficient result as my original table one have 100 columns so I need to use aysteric (*). 我需要高效的结果,因为我的原始表有100列,因此我需要使用aysteric(*)。 I'm currently working in mysql . 我目前在mysql工作。

Use an aggregation query: 使用聚合查询:

SELECT
    t1.id,
    t1.Name,
    t1.Email,
    t1.Address,
    t1.Designation
    COALESECE(t2.expenses, 0) AS Expense
FROM Table1 t1
LEFT JOIN
(
    SELECT user_id, SUM(Expense) AS expenses
    FROM Table2
    GROUP BY user_id
) t2
    ON t1.id = t2.user_id;

We could also write the above without a subquery, using a direct join with aggregation. 我们也可以使用带有聚合的直接联接来编写上面没有子查询的代码。 But, with so many columns having appear in the GROUP BY clause, I prefer the version I gave. 但是,由于GROUP BY子句中出现了许多列,因此我更喜欢给出的版本。

You can directly join sum of expense table of each user by using below query. 您可以使用下面的查询直接加入每个用户的费用总和表。

SELECT userdata.*, (select sum(expense) from expense where expense.user_id=userdata.id) as expense FROM `userdata`  

Or by using left join 或使用左联接

SELECT u.*, sum(e.expense) as expense FROM `userdata` u left join expense e on e.user_id=u.id group by u.id 

Here is the full Example of above query (NOTE - PLEASE CHANGE COLUMN NAME ACCORDING TO YOUR TABLE STRUCTURE) . 这是上述查询的完整示例(根据您的表结构,请注意更改列名称)

User table 用户表

在此处输入图片说明

Expense Table 费用表

在此处输入图片说明

Then run the query 然后运行查询

SELECT userdata.*, (select sum(expense) from expense where expense.user_id=userdata.id) as expense FROM `userdata` 

Response 响应

在此处输入图片说明

暂无
暂无

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

相关问题 如何更新两个mySQL表并将主键从第一个表输入到第二个表? - How to update two mySQL tables and input primary key from first table into second table? 如何从两个表中选择数据,而两个表只为第一表的一行选择了第二表的第一行 - How to SELECT data from two tables which only first row of second table is selected for one row of first table 从两个mysql表中获取数据,其中一个表的行与另一个表的行连接 - Get data from two mysql tables, where row of one table is connected to many in other 读取一个表并从另一个表中获取与第一个表行相关的行 - Read one table and get rows from another table relevant to first tables row 连接 - 从一个表中获取行然后获取另一个表中匹配的所有行? 但不应重复第一个表匹配行 - Joins - get row from one table then get all rows that match in another table? but first table matching row should not be repeated 如何连接两个表,然后显示从另一个表收集的表的每个数据的总和值 - How to join two tables and then display the sum value of each data of a table gathering from the other table 在 MySQL 中,如何从一个表中获取 2 列和从其他表中的一行中获取 2 行作为列? - How to get 2 columns from one table and 2 rows as columns from other table in one row, in MySQL? 使用Codeigniter将表中的一行与MySQL中其他两个表的多行联接 - Join one row from a table with multiple rows of two other tables in MySQL using Codeigniter 从一个表动态获取行并从其他表获取值将其汇总并插入到目标表中 - fetch rows dynamically from one table and get values from other table sum it up and insert into target table mysql join查询从两个表中选择行,一个表有多个与第一个表匹配的行 - mysql join query for select rows from two tables that one table is having multiple rows matching to the first table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM