簡體   English   中英

比較2個表中的數據

[英]Compare data from 2 tables

我不確定這篇文章的標題是否正確,但是我沒有其他想法;)我有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的文檔中獲取與Privileges的記錄相匹配的記錄。 並且有2條記錄-來自文檔的 ID 1和ID 3。 文檔的 ID 1與特權的 ID 1匹配, 文檔的 ID 3與特權的 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條記錄-“ 文檔”中的ID 2與“ 特權 ”中的ID 1相匹配。

對不起我的英語不好 ;)

如果我理解正確,則需要在表之間進行聯接。 但是,當Privileges的值為NULL時,該字段將被忽略(即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';

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'

如果u想要相等的值NULL和空('')值,請使用下面的代碼

Column1 = Column2或Column2為空或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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM