2011-09-07 61 views
0

我正在編寫能夠檢查同一用戶的多行的查詢。如果同一用戶記錄的組合不能提供我需要的信息(請記住它是用戶擁有的所有記錄的組合,而不是一個記錄),則認爲用戶已通過。如何加入多個表的許多條件

例如:有兩個表格。

一種是「用戶」,它保留用戶的個人信息:

id client_id first_name last_name date_of_birth ssn address 

另一種是「篩選」,這使用戶的醫療測試信息:

id user_id date cholesterol ldl hemoglobin triglycerides mcv glucose 
mchc ha1c plateletcount. 

一個用戶只能有一個記錄在用戶表中,但在篩選表中可能有多個記錄。我想要做的是檢查屬於同一用戶的用戶的多個篩選記錄,看看這些記錄的組合是否提供了我需要的必要信息。如果否,則選擇用戶。例如,必要的信息包括膽固醇,ldl,甘油三酯,葡萄糖或。如果用戶有兩個篩選記錄,一個記錄提供cholesterol(NOT NULL),另一個提供triglycerides(NOT NULL),glucose(NOT NULL)ha1c(NOT NULL)。他被選中是因爲ldl缺失。

如何編寫能夠做到這一點的查詢?我嘗試了內部連接,但似乎不起作用。這裏有一些要求。對於膽固醇,ldl和甘油三酯,只要其中一個缺失,應該選擇用戶。但是對於葡萄糖和ha1c,僅當兩者都缺失時才選擇用戶。

一個我想查詢的是這樣的:

SELECT users.id AS user_id, users.first_name, users.last_name, clients.name AS client, 
     users.social_security_number AS ssn, users.hiredate, hra.id AS hra_id, hra.date    AS hra_date, hra.maileddate AS hra_maileddate, 
      screening.id AS screening_id, screening.date AS screening_date,   screening.maileddate AS screening_maileddate 
     FROM users 
     INNER JOIN clients 
     ON(
      users.client_id = clients.id 
      ) 
     INNER JOIN hra 
     ON(
      users.id = hra.user_id 
      ) 
     LEFT JOIN screening 
     ON(
      users.id = screening.user_id 
     ) 
     WHERE users.client_id = '1879'  
     AND hra.date BETWEEN '2011-07-01' AND '2011-11-15' 
     AND hra.maileddate IS NOT NULL 
     AND screening.date BETWEEN '2011-05-15' AND '2011-11-15' 
     AND screening.maileddate IS NULL  
     AND screening.cholesterol IS NOT NULL 
     AND screening.ldl IS NOT NULL 
     AND screening.triglycerides IS NOT NULL 
     AND (screening.glucose IS NOT NULL OR screening.ha1c IS NOT NULL) 
     GROUP BY users.id 

什麼是正確的查詢?

+0

查看WHERE EXIST子句和(顯然)WHERE NOT EXIST來檢查每個客戶端,如果您可以在實驗室中找到一條不合格/限定客戶端的記錄 – Eddy

+0

[如何檢查記錄組合多行(MySQL)](http://stackoverflow.com/questions/7335781/how-to-check-combination-of-records-from-multiple-rowsmysql) –

回答

0

只是動議通過覈對從實驗室到WHERE條件ON

...... 
    LEFT JOIN labs 
    ON(
     users.id = labs.user_id 
     AND labs.date BETWEEN '2011-05-15' AND '2011-11-15' 
     AND labs.maileddate IS NULL  
     AND labs.cholesterol IS NOT NULL 
     AND labs.ldl IS NOT NULL 
     AND labs.triglycerides IS NOT NULL 
     AND (labs.glucose IS NOT NULL OR labs.ha1c IS NOT NULL) 
    ) 
    WHERE users.client_id = '1879'  
    AND hra.date BETWEEN '2011-07-01' AND '2011-11-15' 
    AND hra.maileddate IS NOT NULL 

執行相同的hra,如果你需要想查詢到hra沒有條目返回的用戶(不要忘了,它應該是LEFT JOIN

0

好像你想要的或代替一個與 另外,如果你希望他們選擇的東西丟失時,選擇「IS NULL」,而不是「IS NOT NULL」

你開始與發現,有一個HRA用戶:

WHERE users.client_id = '1879'  
    AND hra.date BETWEEN '2011-07-01' AND '2011-11-15' 

//然後好像你想是這樣的:

AND( labs.cholesterol IS NULL 
OR labs.ldl IS NULL 
OR labs.triglycerides IS NULL 
OR (labs.glucose IS NULL AND labs.ha1c IS NULL)) 
+0

我試過了,但它仍然顯示用戶不應該被選中 –

+0

這些用戶具有哪些屬性使其符合「不應被選中」的屬性? – Zak

0

通過在WHERE子句中的NOT NULL檢查,你戰勝左的目的JOIN和本質,使之成爲一個INNER JOIN ...

SELECT users.id AS user_id, users.first_name, users.last_name, clients.name AS client, 
     users.social_security_number AS ssn, users.hiredate, hra.id AS hra_id, 
     hra.date AS hra_date, hra.maileddate AS hra_maileddate, 
     screening.id AS screening_id, screening.date AS screening_date,    screening.maileddate AS screening_maileddate 
FROM users 
INNER JOIN clients ON(users.client_id = clients.id) 
INNER JOIN hra ON(users.id = hra.user_id) 
-- This says, find matching records in labs, and if there aren't any, then bring 
-- back the fields anyway, filled will NULL's 
LEFT JOIN labs ON(users.id = labs.user_id) 
WHERE users.client_id = '1879'  
AND hra.date BETWEEN '2011-07-01' AND '2011-11-15' 
AND hra.maileddate IS NOT NULL 
-- These in the WHERE clause defeats the purpose of the LEFT JOIN 
-- Move them into the ON condition of the LEFT 
      AND labs.date BETWEEN '2011-05-15' AND '2011-11-15' 
      AND labs.maileddate IS NULL  
      AND labs.cholesterol IS NOT NULL 
      AND labs.ldl IS NOT NULL 
      AND labs.triglycerides IS NOT NULL 
      AND (labs.glucose IS NOT NULL OR labs.ha1c IS NOT NULL) 
GROUP BY users.id 

當LEFT JOIN沒有找到匹配的記錄,它帶回現場s,但是用NULL填充它們的值。

+0

'WHERE子句中的NOT NULL,你擊敗了LEFT JOIN的目的,本質上,使它成爲INNER JOIN ...'不一定如果有問題的字段沒有被定義爲'not null',那麼它可以包含即使在內部聯接中也是null。我認爲這就是OP的意思。 – Johan