在两个表之间提取数据

时间:2014-07-03 08:24:42

标签: sql sql-server

我有两张桌子。一个表是请求类型,另一个是请求权限。根据员工权利,我需要将员工分配的请求类型的所有请求类型显示为true。我想获得特定员工的需求。

例如: -

   Request type table 
  ------------------------

   REQID  REqName   ISACTIVE
---------------------------------  
    1      a           true
    2      b           true
    3      c           true

 REquest Rights table
-------------------------

   ID  ReTYpeId  EmpId  ISActive
 ----------------------------------
   1     1        21      true
   2     2        21      true
   3     1        22      true

  RESult Table 
-----------------------
  REQID   REqName  ISACTIVE
---------------------------------
    1      a           true
    2      b           true
    3      c           false

如何查询这个。

到目前为止,我尝试过这么多

 SELECT tt.TransactionTypeName,tt.TransactionTypeId,tt.IsActive  FROM sTransactionType tt JOIN sTransactionRights tr
 ON tr.TransTypeID=tt.TransactionTypeId
 WHERE tt.Division=1  AND tt.IsActive=1. i tried with case in isactive .but not working at all. 

1 个答案:

答案 0 :(得分:1)

试试这个

SELECT REQID, REqName, ISNULL(r.ISActive, 'false') AS ISACTIVE
FROM [Request type table] t
LEFT JOIN [REquest Rights table] r ON r.ReTYpeId = t.REQID AND EmpId  = 21
WHERE t.ISACTIVE = 'true'