求简单sql - MS-SQL Server / 基础类
table1:
id name sub
1 q w
2 r t
3 y n
table2:
id title_id text
1 3 u
2 2 i
3 1 p
4 2 b
5 1 m
求sql语句 查询出 title_id 相同的项 并且查出在table1中 id = title_id 列的name
结果:
2 2 i r
2 2 b r
2 2 b r 第一个2是不是该为4啊?
你给的数据和结果貌似不对.
SQL code:
select a.id,b.title_id,b.text,a.sub from table1 a join table2 b on a.id=b.title_id
title_id table1中id text name
2 2 b r
2 2 i r
结果不对吧
SQL code:
--结果好像有出入
if not object_id('table1') is null
drop table table1
Go
Create table table1([id] int,[name] nvarchar(1),[sub] nvarchar(1))
Insert table1
select 1,N'q',N'w' union all
select 2,N'r',N't' union all
select 3,N'y',N'n'
Go
if not object_id('table2') is null
drop table table2
Go
Create table table2([id] int,[title_id] int,[t
相关问答:
今天做了一个存储过程 环境是SQL2000数据库
大致如下
建立临时表
定义员工游标
循环员工(属于1个公司)
......
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 ......
请教高手:
以下是数据库中的三条记录,英文为字段名称
id planname TaskBeginTime Status
329 2010年03 ......
原SQL语句SQL code:
SELECT t6.FName '操作工',t1.FDate '日期',t5.FName '制单人',t3.FName '设备',t4.FName '班制',
t7.FBillNo '工艺指令单号',t8.FName '岗位',t2. ......