sql server 2表的问题 [英] sql server 2 tables's issue

查看:64
本文介绍了sql server 2表的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我有一个sql问题,目标是在我的测试网站上显示2个表,第一个显示员工,第二个显示他们所有的工资,这取决于我选择的月份。



i有2张桌子:



员工(身份证,姓名)



薪水(Id,Name,Salary,EmployeeId,DateId)



现在例如,员工表有2个人,Tom和Jerry,薪资表是空的

1.如何查询显示工资表第1行:

hi guys i have a sql issue, objective is to make 2 tables shown in my test site, 1st to show employees, 2nd to show all their salary which depends on the month i selected.

i have 2 tables :

Employee (Id,Name)

Salary (Id,Name,Salary,EmployeeId,DateId)

now for example, Employee table has 2 persons, Tom and Jerry, Salary table is null
1.how to make query to show salary table with 1st row:

name Tom,Salary 0,EmployeeId 1,DateId @selectedate

第2行:

name jerry,Salary 0,EmployeeId 2,DateId @selectedate







2.当我添加新员工时玛丽在员工表中,我也希望在我的工作中显示薪水为0的玛丽工资表,如何制作;而当我从员工表中删除汤姆时,我不会在工资表中看到汤姆,如何制作它?谢谢




2.and when i add a new employee "mary" in employee table, i also want to show mary with salary 0 in my salary table, how to make it; and also when i delete Tom from employee table, i wont see Tom in my salary table, how to make it? thanks

推荐答案

if i understand your issues, then i think you need to use <b>left outer join</b> in your query.
for example,
<pre lang="SQL">Select employee.name,isnull(salary.salary,0),employee.id from employee left outer join salary on
employee.id = salary.employeeid</pre>

for your second issues, if you insert a value to your first table without inserting value to second table,above sql (left outer join) will return salary as 0.







i认为它会解决您的问题。如果不是,请告诉我。




i think it will solve your issues. let me know, if it not.


这篇关于sql server 2表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆