求一个查询语句(在asp中用的)

skyearth 2003-12-18 08:41:27
表一
user id paw
物理系 200 666
电子实验室 20001 666
光学实验室 20002 666
化学系 300 666
无机实验室 30001 666
..........
表二
device id
二极管 20001
透镜 20002
.........
希望实现
通过物理系...这用户名登陆将所有物理系...设备列出,通过实验室用户名登陆只列出相关设备,请高手指教,谢
...全文
48 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-12-18
  • 打赏
  • 举报
回复
--针对楼主的数据存储方式,还可以简化为:
create proc p_qry
@user nvarchar(10)
as
declare @id varchar(11)
select @id=id+'%' from t1 where [user]=@user

select * from t2 where id like @id
go
hillhx 2003-12-18
  • 打赏
  • 举报
回复
用LIKE
SELECT * FROM 表一,表二
WEHERE 表二.id = 表一.ID
AND 表一.id LIKE 你传入的部门ID+‘%’
zjcxc 元老 2003-12-18
  • 打赏
  • 举报
回复
--借用马可的测试

--测试:
create table t1([user] nvarchar(10),id varchar(10),paw int)
insert t1 select '物理系','200',666
union all select '电子实验室','20001', 666
union all select '光学实验室','20002',666
union all select '化学系','300',666
union all select '无机实验室','30001',666
create table t2(device nvarchar(10),id varchar(10))
insert t2 values('二极管','20001')
insert t2 values('透镜','20002')
go

--用存储过程
create proc p_qry
@user nvarchar(10)
as
declare @id varchar(11)
select @id=id+'%' from t1 where [user]=@user

select * from t2 a join t2 b on a.id=b.id
where b.id like @id
go

--调用
exec p_qry '物理系'
go

--删除测试环境
drop table t1,t2
drop proc p_qry

/*--测试结果
device id device id
---------- ---------- ---------- ----------
二极管 20001 二极管 20001
透镜 20002 透镜 20002

(所影响的行数为 2 行)
--*/
zjcxc 元老 2003-12-18
  • 打赏
  • 举报
回复
--借用马可的测试

--测试:
create table t1([user] nvarchar(10),id varchar(10),paw int)
insert t1 select '物理系','200',666
union all select '电子实验室','20001', 666
union all select '光学实验室','20002',666
union all select '化学系','300',666
union all select '无机实验室','30001',666
create table t2(device nvarchar(10),id varchar(10))
insert t2 values('二极管','20001')
insert t2 values('透镜','20002')
go

--用存储过程
create proc p_qry
@user nvarchar(10)
as
declare @id varchar(11)
select @id=id+'%' from t1 where [user]=@user

select * from t2 a join t2 b on a.id=b.id
where b.id like @id
go

--调用
exec p_qry '物理系'
go

--删除测试环境
drop table t1,t2
drop proc p_qry

/*--测试结果
device id device id
---------- ---------- ---------- ----------
二极管 20001 二极管 20001
透镜 20002 透镜 20002

(所影响的行数为 2 行)
--*/
zjcxc 元老 2003-12-18
  • 打赏
  • 举报
回复
--这样简单一点,不用计算那么多次.

--用存储过程
create proc p_qry
@user nvarchar(10)
as
declare @id varchar(11)
select @id=id+'%' from t1 where [user]=@user

select * from t2 a join t2 b on a.id=b.id
where b.id like @id
go

--调用
exec p_qry '物理系'
victorycyz 2003-12-18
  • 打赏
  • 举报
回复

declare @i int
select @i=id from table1 where user='用户登录的user'

select a.*,b.device
from table1 a join table2 on a.id=b.id
where a.id like @i+'%'
yuncai 2003-12-18
  • 打赏
  • 举报
回复
select * from 表二 where id =(select id from 表一 where user=物理系)
txlicenhe 2003-12-18
  • 打赏
  • 举报
回复
--测试:
create table t1([user] nvarchar(10),id varchar(10),paw int)
insert t1 select '物理系','200', 666
union all select '电子实验室','20001', 666
union all select '光学实验室','20002', 666
union all select '化学系','300', 666
union all select '无机实验室','30001', 666
create table t2(device nvarchar(10),id varchar(10))
insert t2 values('二极管','20001')
insert t2 values('透镜','20002')
go
-- 用存储过程
create proc test @user nvarchar(10)
as
declare @id varchar(10)
select @id = id from t1 where [user] = @user
select * from t1 a
join t2 b on left(a.id,len(@id)) = left(b.id,len(@id)) and left(a.id,len(@id)) = @id
--调用
exec test '物理系'
/* 显示结果
user id paw device id
---------- ---------- ----------- ---------- ----------
物理系 200 666 二极管 20001
电子实验室 20001 666 二极管 20001
光学实验室 20002 666 二极管 20001
物理系 200 666 透镜 20002
电子实验室 20001 666 透镜 20002
光学实验室 20002 666 透镜 20002

(所影响的行数为 6 行)
*/
--调用
exec test '电子实验室'
/* 显示结果
user id paw device id
---------- ---------- ----------- ---------- ----------
电子实验室 20001 666 二极管 20001

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

34,587

社区成员

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

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