简体   繁体   English

如何从一个连接语句中的左表中获取所有行?

[英]How to get all rows from left table in a join statement?

I have two tables. 我有两张桌子。 One is Employee_Mstr and other is EmployeeLeaveRequest_mstr . 一个是Employee_Mstr ,另一个是EmployeeLeaveRequest_mstr

My data: 我的资料:

我的资料

Use GROUP BY . 使用GROUP BY

select t1.emp_cd, t1.emp_name, sum(t2.num_dy) from 
  Employee_Mstr t1 left join 
  EmployeeLeaveRequest_mstr t2 on t1.emp_cd = t2.emp_cd group by t1.emp_cd, t2.emp_name;
SELECT tab1.emp_cd, tab1.emp_name, SUM(tab2.num_dy)
FROM Employee_Mstr tab1, "2nd Table Records" tab2
where tab1.emp_cd = tab2.emp_cd
group by tab1.emp_cd, tab1.emp_name;

Should work, just set the correct table name for the 2nd table. 应该起作用,只需为第二张表设置正确的表名。

Make pk emp_cd the primary key, and then join the tales using left join . pk emp_cd用作主键,然后使用left join加入故事。 You can then use sumof to add values, use groupby before adding the values. 然后,您可以使用sumof添加值,在添加值之前使用groupby

If you want, just check w3school.com. 如果需要,请访问w3school.com。

select mstr.Emp_cd, mstr.Emp_Name, isnull(sum(det.num_dy), 0) as num_day
from dbo.Employee_Mstr mstr
left join dbo.Employee_Nums det on (mstr.Emp_cd = det.Emp_cd)
group by mstr.Emp_cd, mstr.Emp_Name;

should do the job. 应该做的工作。

Here it's results of SQL execution: 这是SQL执行的结果:

Emp_cd      Emp_Name
----------- --------------------------------------------------
1           A
2           B
3           C

(3 row(s) affected)

Emp_cd      num_dy
----------- -----------
3           4
3           2

(2 row(s) affected)

Emp_cd      Emp_Name                                           num_day
----------- -------------------------------------------------- -----------
1           A                                                  0
2           B                                                  0
3           C                                                  6
Warning: Null value is eliminated by an aggregate or other SET operation.

(3 row(s) affected)

Try this: 尝试这个:

select t1.cd, t1.name, sum(t2.num_dy) from employee_Mstr t1 
left join EmployeeLeaveRequest_mstr t2 on t1.cd = t2.cd group by t1.cd, t1.name;

暂无
暂无

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

相关问题 MySql join语句与左表中的所有行 - MySql join statement with all rows in left table 在SQL连接操作中,如何从左侧联接中获取行,以及如何从右侧表中获取两列的汇总 - In an SQL join operation, how to get the rows from the left join and only the aggregate of two columns from the right table 如何左加入一个表而不保留左表中的所有记录? - How to LEFT JOIN a table not keeping all the records from left table? 查询左联接而没有B表中的所有右行 - Query left join without all the right rows from B table Oracle:如何使用左外部联接从左表获取所有条目并满足Where子句中的条件 - Oracle: How to use left outer join to get all entries from left table and satisfying the condition in Where clause 如何通过从左表中获取所有行并仅在右表中匹配来连接 2 个表 - How to join 2 tables with getting all rows from left table and only matching ones in right Oracle,LEFT OUTER JOIN不返回左表中的所有行,而是表现得像INNER JOIN - Oracle, LEFT OUTER JOIN not returning all rows from left table, instead behaving like INNER JOIN LEFT JOIN 从左表中获取所有行,而不管连接条件 - LEFT JOIN to take all rows from left table irrespective of join condition 如何进行左连接以获取带有参数的所有行? - How to do left join to get all the rows with param? 左联接并显示左表中的所有行;如果右表中不存在,则为null - left join and show all rows from left table and null if not exist on right table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM