我不确定这篇文章的标题是否正确,但我没有任何其他想法;) 我有2个表 - 文档和权限。第一个是由4个属性描述的文档记录(客户,项目,文档类别,文档类型) 。在第二个是由相同属性+ 用户
描述的特权记录示例1
文档
+----+----------+---------+-------------------+---------------+
| ID | Customer | Project | Document category | Document type |
+----+----------+---------+-------------------+---------------+
| 1 | CUST1 | PROJ1 | CAT1 | TYPE1 |
| 2 | CUST1 | PROJ2 | CAT2 | TYPE1 |
| 3 | CUST2 | PROJ1 | CAT2 | TYPE2 |
+----+----------+---------+-------------------+---------------+
权限
+----+----------+---------+-------------------+---------------+-------+
| ID | Customer | Project | Document category | Document type | User |
+----+----------+---------+-------------------+---------------+-------+
| 1 | CUST1 | | CAT1 | | USER1 |
| 2 | | PROJ1 | CAT2 | | USER1 |
| 3 | | PROJ1 | CAT2 | | USER2 |
+----+----------+---------+-------------------+---------------+-------+
现在,我想从USER1的 Documents 获取与 Privileges 中的记录匹配的记录。并且有2条记录 - 来自 Documents 的ID 1和ID 3。 Documents 中的ID 1与 Privileges 中的ID 1匹配, Documents 中的ID 3与 Privileges 中的ID 2匹配。 权限中的值必须相同或为空/空。
示例2
文档
+----+----------+---------+-------------------+---------------+
| ID | Customer | Project | Document category | Document type |
+----+----------+---------+-------------------+---------------+
| 1 | CUST1 | PROJ1 | CAT1 | TYPE1 |
| 2 | CUST1 | PROJ2 | CAT2 | TYPE1 |
| 3 | CUST2 | PROJ1 | CAT2 | TYPE2 |
+----+----------+---------+-------------------+---------------+
权限
+----+----------+---------+-------------------+---------------+-------+
| ID | Customer | Project | Document category | Document type | User |
+----+----------+---------+-------------------+---------------+-------+
| 1 | CUST1 | | CAT2 | | USER1 |
| 2 | CUST1 | PROJ1 | CAT1 | DTYPE2 | USER1 |
+----+----------+---------+-------------------+---------------+-------+
现在只有1条记录 - Documents 中的ID 2与 Privileges 中的ID 1匹配。'
对不起我的英文;)
答案 0 :(得分:1)
如果我理解正确,您需要在表之间进行连接。但是,当NULL
中的值为Privileges
时,该字段将被忽略(即NULL
暗示“全部”)。
一种方法是查询,例如:
select d.*
from documents d join
privileges p
on (d.customer = p.customer or p.customer is null) and
(d.project = p.project or p.project is null) and
(d.documentcategory = p.documentcategory or p.documentcategory is null) and
(d.documenttype = p.documenttype or p.documenttype is null)
where p.user = 'USER1';
答案 1 :(得分:0)
Column1 = Column2或Column2为空
select dc.* from Documents dc join Privileges pr on
(dc.Customer =pr.Customer or pr.Customer is null) and
(dc.Project = pr.Project or pr.Project is null) and
(dc.Document_category = pr.Document_Category or pr.Document_Category is null) and
(dc.Document_Type =pr.Document_Type or pr.Document_Type is null)
where pr.[User]='User1'
如果你想要相等的值NULL和空('')值,请使用以下代码
Column1 = Column2或Column2为null或Column2 =''
select dc.* from Documents dc join Privileges pr on
(dc.Customer =pr.Customer or pr.Customer is null or pr.Customer='') and
(dc.Project = pr.Project or pr.Project is null or pr.Project='') and
(dc.Document_category = pr.Document_Category or pr.Document_Category is null or pr.Document_Category='') and
(dc.Document_Type =pr.Document_Type or pr.Document_Type is null or pr.Document_Type='')
where pr.[User]='User1'