从2个表中获取信息

时间:2015-12-07 02:15:52

标签: sql sql-server

我有以下2个表格:

表1

Organization Location
001          110 
001          112
001          113
002          119
002          121

表2

 UserId    Location
 User1      110
 User1      112
 User2      121

我的参数是Organization和UserId。表2是限制访问的表。例如,对于User1,他只能看到位置110和112的信息。 User2只能查看位置121的信息。

注意: 表2中没有的User3可以看到给定Organization的所有位置的信息。例如,对于Organization 001,用户3可以看到位置110,112,113的信息。还要注意,表2中没有的任何用户都是没有限制,所以我们看到给定位置的所有位置。

如何执行参数为Organization和UserId的单个select语句。根据该查询,我想返回位置。我知道需要加入,但不知道如何构建它以实现目标。

当我将其输入ASP.NET SQLSource中的SelectCommand时,我无法使用IF语句。我需要使用一个select语句。我正在使用SQL Server作为数据库。

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您可以像这样使用查询:

select distinct t2.location
from table2 t2
inner join table1 t1 
  on t2.location = t1.location
  and '001' = t1.organization
  and t2.userid = 'User1' 

对于给定的' 001'组织和' User1',您可以通过在公共位置字段中加入两个表来获取位置。

此查询也可以写为:

select distinct t2.location
from table2 t2
inner join table1 t1 
  on t2.location = t1.location
where 
  t1.organization = '001'
  and t2.userid = 'User1' 

编辑:

存储过程可能会对您有所帮助:

delimiter //

drop procedure if exists get_locations//

create procedure get_locations (
    p_organization char(3),
    p_userid varchar(20)
)
begin

    -- find if there are any users corresponding
    -- to the user provided as an argument
    -- (see notes about org 001 and user2 below)
    select count(*) into @count_user
    from table2 where userid = p_userid;

    -- if org 001 and user User2 are provided, can they see
    -- all locations for 001? If the answer is yes, use the
    -- query below (commented) instead of the one above
    /*
    select count(*) into @count_user
    from table2 t2
    inner join table1 t1 on t1.location = t2.location
    where 
      t1.organization = p_organization 
      and t2.userid = p_userid;
    */

    -- if no user is found, show all location for that org
    if @count_user = 0 then
        select distinct location
        from table1
        where organization = p_organization;

    -- otherwise, get the right location for org and user
    else
        select distinct t1.location
        from table2 t2
        inner join table1 t1 on t1.location = t2.location
        where
            t1.organization = p_organization
            and t2.userid = p_userid;
    end if;

end//

delimiter ;

结果:

call get_locations('001','User1');
+----------+
| location |
+----------+
|      110 |
|      112 |
+----------+

call get_locations('001','User2');
Empty set (0.00 sec)

call get_locations('001','User3');
+----------+
| location |
+----------+
|      110 |
|      112 |
|      113 |
+----------+

编辑2:仅使用select语句:

select distinct t2.location
from table2 t2
inner join table1 t1 
  on t2.location = t1.location
where 
  t1.organization = '001'
  and t2.userid = 'User3'

union

select distinct t2.location
from table2 t2
where 0 = (
  select count(*)
  from table2
  where userid = 'User3'
);

将导致所有3个地点

select distinct t2.location
from table2 t2
inner join table1 t1 
  on t2.location = t1.location
where 
  t1.organization = '001'
  and t2.userid = 'User1'

union

select distinct t2.location
from table2 t2
where 0 = (
  select count(*)
  from table2
  where userid = 'User1'
)

将产生2个位置。

答案 1 :(得分:0)

这可以通过简单的union

来完成
select top 1 with ties from 
    (select t1.location, 1 as level
     from table1 t1
     join table2 on t1.location = t2.location
     where t2.userid = 'user' and t1.organization = '001'

     union all

     select location, 2 as level
     from table1
     where organization = '001') t
order by level

如果限制表为用户保留了一些行,它将只选择那些行。否则,它将从table1中选择所有位置。