请教这个SQL语句如何写?另想问,在子语句里面可以获得父语句的字段值吗?

upshania 2009-04-24 05:17:19
project表

id projectName flag

0 项目1 true
1 项目2 true
2 项目3 false
3 项目4 true

Test1表

id name projectid
1 张三 0
2 张三 1


Test2表

id name projectid
1 李四 2
2 李四 3



我想通过一个SQL,,外部传入一个flag 变量到project表,如"true" 或者"false"就可以得到下面的结果表,问怎么得到


如:传入true,结果如下:

项目1 张三
项目2 张三
项目4 李四


传入flase 结果如下:

项目3 李四



另外我还想问一下,SQL里面可以执行循还语句吗?






这里存下查询的记录 = select * from project where flag = true

然后某个子语句可以执行 select * from test1 where projectid = 上面那个语句的结果集id字段



应用程序里面是可以通过执行多条语句来实现这一操作,可是在SQL里面可以通过一个SQL语句得到吗?,请教您,谢谢











...全文
92 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hnqjc 2009-04-24
  • 打赏
  • 举报
回复
学习了...
sdhdy 2009-04-24
  • 打赏
  • 举报
回复
--循环那个可以用游标。
--用游标
declare @id int
declare DZCursor CURSOR for select id from project where flag = true
open DZCursor
fetch next from DZCursor into @id
while @@fetch_status=0
begin
select * from test1 where projectid =@id
fetch next from DZCursor into @id
end
close DZCursor
deallocate DZCursor
sdhdy 2009-04-24
  • 打赏
  • 举报
回复
create table project(id int,   projectName varchar(10),    flag varchar(10))
go
insert project select 0 , '项目1' , 'true'
insert project select 1 , '项目2' , 'true'
insert project select 2 , '项目3' , 'false'
insert project select 3 , '项目4' , 'true'


create table test1(id int, name varchar(10), projectid int)
go
insert test1 select 1 , '张三' , 0
insert test1 select 2 , '张三' , 1
go
create table test2(id int, name varchar(10), projectid int)
insert test2 select 1 , '李四' , 2
insert test2 select 2 , '李四' , 3
go
--传入true,结果如下:

select b.projectname,a.name from test1 a,project b where a.projectid=b.id and a.projectid in (select id from project where flag='true')
union all
select b.projectname,a.name from test2 a,project b where a.projectid=b.id and a.projectid in (select id from project where flag='true')
/*
projectname name
----------- ----------
项目1 张三
项目2 张三
项目4 李四

(所影响的行数为 3 行)
*/

--传入flase 结果如下:
select b.projectname,a.name from test1 a,project b where a.projectid=b.id and a.projectid in (select id from project where flag='false')
union all
select b.projectname,a.name from test2 a,project b where a.projectid=b.id and a.projectid in (select id from project where flag='false')
/*
projectname name
----------- ----------
项目3 李四

(所影响的行数为 1 行)
*/
htl258_Tony 2009-04-24
  • 打赏
  • 举报
回复
if object_id('project') is not null drop table project 
go
create table project([id] int,[projectName] varchar(10),FLAG varchar(10))
insert project select 0,'项目1','TRUE'
union all select 1,'项目2','TRUE'
union all select 2,'项目3','FALSE'
union all select 3,'项目4','TRUE'
go
if object_id('test1') is not null drop table test1
go
create table test1([id] int,[name] varchar(10),[projectid] int)
insert test1 select 1,'张三',0
union all select 2,'张三',1
go
if object_id('test2') is not null drop table test2
go
create table test2([id] int,[name] varchar(10),[projectid] int)
insert test2 select 1,'李四',2
union all select 2,'李四',3
go
select a.projectName,b.Name
from project a
join (select * from test1 union all select * from test2) b
on a.id=b.[projectid]
where flag='true'
/*
projectName Name
----------- ----------
项目1 张三
项目2 张三
项目4 李四

(3 行受影响)
*/
select a.projectName,b.Name
from project a
join (select * from test1 union all select * from test2) b
on a.id=b.[projectid]
where flag='false'
/*
projectName Name
----------- ----------
项目3 李四

(1 行受影响)
*/
upshania 2009-04-24
  • 打赏
  • 举报
回复
楼上的,如果我的Test2表和Test1表完全不同呢,如果这下还有个Test3表





Test3表

id xxx projectid
1 空 2
2 空 3


htl258_Tony 2009-04-24
  • 打赏
  • 举报
回复
多表连接查询.
llsen 2009-04-24
  • 打赏
  • 举报
回复

declare @flag int
set @flag = 1 -- or 0

select A.projectName,B.name
from project as A
LEFT JOIN Test1 AS B
ON A.projectid = B.projectid
WHERE A.flag = @flag
UNION ALL
select C.projectName,D.name
from project as C
LEFT JOIN Test1 AS D
ON C.projectid = D.projectid
WHERE C.flag = @flag

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧