数据库实战2

上一篇 / 下一篇  2017-07-25 20:22:30 / 个人分类:数据库

题目描述

查找各个部门当前(to_date='9999-01-01')领导当前薪水详情以及其对应部门编号dept_no
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));

输入描述:

输出描述:

emp_nosalaryfrom_dateto_datedept_no
10002725272001-08-029999-01-01d001
10004740572001-11-279999-01-01d004
10005946922001-09-099999-01-01d003
10006433112001-08-029999-01-01d002
10010944092001-11-239999-01-01d006 
答案一:
select s.*,d.dept_no from salaries s,dept_manager d where  d.emp_no = s.emp_no and d.to_date='9999-01-01' and s.to_date='9999-01-01'
答案二:
select s.*,d.dept_no from salaries s inner join dept_manager d on  d.emp_no = s.emp_no where d.to_date='9999-01-01' and s.to_date='9999-01-01'

left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 
right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
inner join(等值连接) 只返回两个表中联结字段相等的行
语法:select* from 表1 left/right/inner join  表2 on  表1.字段号=表2.字段号



TAG:

 

评分:0

我来说两句

日历

« 2024-03-29  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 5915
  • 日志数: 9
  • 建立时间: 2016-02-03
  • 更新时间: 2017-07-28

RSS订阅

Open Toolbar