27,580
社区成员
发帖
与我相关
我的任务
分享
--联接查询实例:
go
if OBJECT_ID('emp')is not null
drop table emp
go
create table emp(
id int,
deptid int,
sex varchar(2)
)
go
insert emp
select 1,1,'男' union all
select 2,2,'男' union all
select 3,2,'女' union all
select 4,1,'男' union all
select 6,4,'男' union all
select 5,3,'女'
go
if OBJECT_ID('dept')is not null
drop table dept
go
create table dept(
deptid int,
deptname varchar(10)
)
go
insert dept
select 1,'sales' union all
select 2,'production' union all
select 3,'market'
--查询每个员工的id,部门名字
--方法1
select id,deptname from emp,dept where emp.deptid=dept.deptid
--或者:
select id,deptname from emp inner join dept on emp.deptid=dept.deptid
/*
id deptname
1 sales
2 production
3 production
4 sales
5 market
*/
--主意,以上查询没有返回emp的员工的所有信息
--使用left join
select id,deptname from emp left join dept on emp.deptid=dept.deptid
/*
id deptname
1 sales
2 production
3 production
4 sales
6 NULL
5 market
*/
--此时返回了emp表的所有信息
/*
包括部门,姓名,出勤天数,迟到次数,早退次数,矿工次数 这些数据
*/
go
if OBJECT_ID('dept') is not null
drop table dept
go
create table dept(
deptid varchar(5),
deptname varchar(20)
)
go
if OBJECT_ID('emp') is not null
drop table emp
go
create table emp(
empid varchar(10),
empname varchar(20),
empsex varchar(5) check(empsex in('men','women')),
empage int,
birthday datetime,
addr varchar(50),
tel varchar(12),
deptid varchar(5)
)
go
if OBJECT_ID('kaoqin') is not null
drop table kaoqin
go
create table kaoqin(
empid varchar(10),
deptid varchar(5),
出勤天数 int,
迟到次数 int,
早退次数 int,
矿工次数 int
)
--包括部门,姓名,出勤天数,迟到次数,早退次数,矿工次数
select
dept.deptname as 部门,emp.empname as 姓名,
出勤天数,迟到次数,早退次数,矿工次数
from emp
inner join dept on emp.deptid=dept.deptid
inner join kaoqin on emp.empid=kaoqin.empid
/*
包括部门 姓名 出勤天数 迟到次数 早退次数 矿工次数
*/
--没有插入数据,你可插入一点以试试
/*
父件 子件 替代件
T088-02 A C
T088-03 A C
T089-00 B D
T089-03 B D
T091-00 F E
T099-01 F D
T088-02 (02是版本号,T088是机种名称,我想让这个表显示最大版本号的相关信息)
结果:
T088-03 A C
T089-03 B D
T099-01 F D
*/
go
if OBJECT_ID('tbl') is not null
drop table tbl
go
create table tbl(
父件 varchar(10),
子件 varchar(2),
替代件 varchar(2)
)
go
insert tbl
select 'T088-02','A','C' union all
select 'T088-03','A','C' union all
select 'T089-00','B','D' union all
select 'T089-03','B','D' union all
select 'T091-00','F','E' union all
select 'T099-01','F','D'
select *from tbl where
CAST(right(父件,2) as int) in (select a from(
select max(CAST(right(父件,2) as int)) as a,子件,替代件 from tbl group by 子件,替代件)b)
/*
示例:
ID 单别 单号 金额 赋值金额 赋值单号 检索条件
01 1 1 50 2-2
02 2 2 100
if 单别=LEFT(检索条件,1) and 单号=RIGHT(检索条件,2)
BEGIN UPDATE 赋值金额= 金额,赋值单号=单别+'-'+单号
最后 result:
ID 单别 单号 金额 赋值金额 赋值单号 检索条件
01 1 1 50 2-2
02 2 2 100 50 1-1
咋用UPDATE 实现这样的效果呢!
*/
if
OBJECT_ID('tbl')is not null
drop table tbl
go
create table tbl(
ID varchar(4),
单别 varchar(4),
单号 varchar(4),
金额 numeric(6,2),
赋值金额 numeric(6,2),
赋值单号 varchar(10),
检索条件 varchar(10)
)
go
insert tbl (ID,单别,单号,金额)
select '01','1','1',50
union all
select '02','2','2',100
update tbl
set 赋值金额=a.金额,赋值单号=a.单别+'-'+a.单号
from tbl a where a.ID=ID
/*
ID 单别 单号 金额 赋值金额 赋值单号 检索条件
01 1 1 50.00 50.00 1-1 NULL
02 2 2 100.00 100.00 2-2 NULL
*/
--楼主的条件不怎么明白
/*
表如下
名称 最新统计时间 剩余数量
苹果 2012-2-7 20
苹果 2012-2-5 50
苹果 2012-2-1 40
橘子 2012-2-2 0
橘子 2012-2-1 30
橘子 2012-1-31 10
香蕉 2011-12-1 8.
菠萝
想统计各个水果类型最新日期的剩余数量,
得到如下数据
苹果 20
橘子 0
香蕉 8
菠萝 5
*/
go
if OBJECT_ID('tbl') is not null
drop table tbl
go
create table tbl(
name nvarchar(10),
[time] date,
number int
)
go
insert into tbl
select '苹果' ,'2012-2-7', 20 union
select '苹果' ,'2012-2-5', 50 union
select '苹果' ,'2012-2-1', 40 union
select '橘子' ,'2012-2-2', 0 union
select '橘子' ,'2012-2-1', 30 union
select '橘子' ,'2012-1-31', 10 union
select '香蕉' ,'2011-12-1', 8 union
select '菠萝','2011-12-1',5
;with T
as
(
select * from tbl a
where [time] =
(select MAX([time]) from tbl b where a.name=b.name)
)
select *from T
/*
结果:
name time number
香蕉 2011-12-01 8
苹果 2012-02-07 20
橘子 2012-02-02 0
菠萝 2011-12-01 5
*/
连接查询的一些实例
declare @Task table (组织者 int)
insert into @Task
select 1 union all
select 2 union all
select 3 union all
select null
declare @Reviewer table (姓名 int)
insert into @Reviewer
select 3 union all
select 4 union all
select 5 union all
select null
select * from @Task a,@Reviewer b where a.组织者=b.姓名
/*
组织者 姓名
----------- -----------
3 3
(1 row(s) affected)
*/