日期:2014-05-17  浏览次数:20371 次

连接查询,主表中多字段是在一个连表中
user表 
用户ID  
用户姓名

work表

操作人:
检测人:
维修人:
录入时间:


work表中的“ 操作人:检测人:维修人:” 都存的是user表中的用户ID,

想要的结果:  

操作人    检测人    维修人     录入时间
小王      小李      小刘       2013-1-1
小王      小李      小李       2013-1-1
小王      小刘      小刘       2013-1-1

这样的查询语句该怎么写?  
sql?连接查询语句

------解决方案--------------------
select u1.姓名, u2.姓名, u3.姓名, w.时间 
from user u1, user u2, user u3, worker w
where w.操作人 = u1.id
and w.检测人 = u2.id
and w.维修人 = u3.id
------解决方案--------------------
你倒搞掂数据来测一下嘛
----------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2013-03-07 22:15:26
-- Version:
--      Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86) 
-- Jun 17 2011 00:57:23 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------
--> 测试数据:[user]
if object_id('[user]') is not null drop table [user]
go 
create table [user]([userID] int,[userName] varchar(1))
insert [user]
select 1,'a' union all
select 2,'b' union all
select 3,'c' union all
select 4,'d' union all
select 5,'e'

--> 测试数据:[work]
if object_id('[work]') is not null drop table [work]
go 
create table [work]([oid] int,[cid] int,[mid] int,[intime] datetime)
insert [work]
select 1,2,3,'2013-1-1' union all
select 2,3,4,'2013-1-1' union all
select 4,5,2,'2013-1-1'
--------------开始查询--------------------------

select b.username,c.username,d.username,a.intime from [work] a INNER JOIN [user]b ON a.oid=b.userid
INNER JOIN [user] c ON a.oid=c.userid
INNER JOIN [user] d ON a.oid=d.userid
--------------开始查询--------------------------


----------------结果----------------------------
/* 
username username username intime
-------- -------- -------- -----------------------
a        a        a        2013-01-01 00:00:00.000
b        b        b        2013-01-01 00:00:00.000
d        d        d        2013-01-