查询不排除具有特定权限的人

Query not excluding people with certain privilege(查询不排除具有特定权限的人)
本文介绍了查询不排除具有特定权限的人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有这样的数据:表 Person_by_Privileges:

<前>PersonID Last First 设施部门特权1 霍夫玛丽 S P abc1 霍夫玛丽 S P cde1 霍夫玛丽 SP def2 史密斯乔治 S P abc2 Smith Georg S P cde

我试图让查询只返回 Georg Smith,因为他没有 def 的权限.我在这样做时遇到了麻烦,因为它是不同的查询行.我正在尝试:

SELECT 不同的 [PersonID],[最后的],[第一的],[设施],[部门],权限=东西((SELECT ',' + pp.Privileages FROM Person_by_Privilages pp where Facility='S' FOR XML PATH ('')), 1, 1, '')来自 [Person_by_Privileges] pp在哪里设施='S'和(部门喜欢 ('%p%'))和不喜欢的权限 ('%def%')

但是当我查看该查询的第一个人 (Hoff) 时,不应返回该信息,因为他们确实拥有 def 的权限,我确实在结果中找到了该人:

PersonID Last First Facility Dept 特权1 Hoff Mary SP(她的特权列表附加在一起......确实包括def)

解决方案

您想显示用户行,前提是不存在具有定义权限的行.类似的东西

SELECT personid、last、first、facility、department、...FROM person_by_privilages ppWHERE 设施 = 'S'AND 部门喜欢 ('%P%')并且不存在(选择 *来自 person_by_privilages pp2哪里 pp2.personid = pp.personidAND pp2.facility = 'S'AND pp2.department like ('%P%')AND pp2.privilages LIKE ('%def%'));

I have data like this: table Person_by_Privileges:

PersonID Last  First Facility Department Privilages
1        Hoff  Mary  S        P          abc
1        Hoff  Mary  S        P          cde
1        Hoff  Mary  S        P          def
2        Smith Georg S        P          abc
2        Smith Georg S        P          cde

I'm trying to get the query to only return Georg Smith, since he doesn't have Privilege for def. I'm having trouble doing that, since it's different query lines. I'm trying this:

SELECT distinct [PersonID]
      ,[Last]
      ,[First]
      ,[Facility]
      ,[Department]
      ,Privilages = STUFF(
                 (SELECT ',' + pp.Privilages FROM Person_by_Privilages pp where Facility='S' FOR XML PATH ('')), 1, 1, ''
               ) 
  FROM [Person_by_Privilages] pp  
  where
  Facility='S'
  AND
  (
     Department like ('%p%')
  )
  and 
  Privilages NOT LIKE ('%def%')

But when I look at the first person (Hoff) for that query, that shouldn't be returned because they do have Privilage for def, I do get that person in the results:

PersonID Last First Facility Dept Privilages
1        Hoff Mary  S        P    (list of her Privilages appended together..does include the def)

解决方案

You want to show a users rows, provided there does not exist a row with a def privilege. Something like

SELECT personid, last, first, facility, department, ...
FROM person_by_privilages pp
WHERE facility = 'S'
AND department like ('%P%')
AND NOT EXISTS
(
  SELECT *
  FROM person_by_privilages pp2
  WHERE pp2.personid = pp.personid
  AND pp2.facility = 'S'
  AND pp2.department like ('%P%')
  AND pp2.privilages LIKE ('%def%')
);

这篇关于查询不排除具有特定权限的人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)