求一条SQL语句 - MS-SQL Server / 基础类
现有
表A 表B
id name firid secid thiid
1 军1 1 3 5
2 军2 1 3 6
3 师1 2 4 7
4 师2
5 旅1
6 旅2
7 旅3
想得到如下结果
fir sec thi
军1 师1 旅1
军1 师1 旅2
军2 师2 旅3
怎样实现呢?谢谢!
SQL code:
SELECT A1.[NAME],A2.[NAME],A3.[NAME]
from B
INNER JOIN A A1 ON B.FIR=A1.ID
INNERJ OIN A A2 ON B.SEC=A2.ID
INNER JOIN A A3 ON B.THI=A3.ID
SQL code:
SELECT A1.[NAME],A2.[NAME],A3.[NAME]
from B
INNER JOIN A A1 ON B.FIR=A1.ID
INNER JOIN A A2 ON B.SEC=A2.ID
INNER JOIN A A3 ON B.THI=A3.ID
测试
SQL code:
create table ta (id int,name varchar(3))
insert ta
select 1 ,'军1' union all
select 2 ,'军2' union all
select 3 ,'师1' union all
select 4 ,'师2' union all
select 5 ,'旅1' union all
select 6 ,'旅2' union all
select 7 ,'旅3'
create table tb(firid int, secid int,thiid int)
insert tb
select 1,3,5 union all
select 1,3,6 union all
select 2,4,7
select t1.name fir,t2.name sec,t3.name thi
from tb,ta t1
相关问答:
现在有两张表:文章主表A(articleId,articleTitle),文章评论表B(commentId,articleId,commentTitle)
现在我想实现这样的功能:列出文章列表,其中每篇文章标题下面列出此文章的前2个文章评论,请问sql语句怎么写啊 ......
如何在SQL2005中设定定时作业,比如说定时清理某些表的数据,
或者是定时的将某些表的数据导出excel!
在线等待,急急急,最好是详细步骤!
之前我做的作业有点问题!
帮UP
参考:http://hi.baidu.com/toiota ......
tab1 字段:billdate,goodsid,incount,inmoney,outcount,outmoney,endprice,endcount,endamt
tab2 字段:goodsid,goodskind(商品类型)
tab3 字段:goodskind(商品类型),kindname
结果:
得到商品类型在一段时间 ......
字段1,字段2.....字段N,Status,ParentID
1,Name1....test1,1,99
1,Name1....test1,3,99
1,Name2....test2,1,101
1,Name2....test2,3,101
1,Name3....test3,2,101
1,Name1....test1,4,101
想要的结果是:
1,Na ......
原SQL语句SQL code:
SELECT t6.FName '操作工',t1.FDate '日期',t5.FName '制单人',t3.FName '设备',t4.FName '班制',
t7.FBillNo '工艺指令单号',t8.FName '岗位',t2. ......