2011-09-08 103 views
0

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

例如:有兩個表格。

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

ID CLIENT_ID LAST_NAMEFIRST_NAME您好DATE_OF_BIRTH SSN解決 另一種是「篩選」,這使用戶的醫療測試信息:

ID user_ID的日期膽固醇LDL血紅蛋白甘油三酯mcv葡萄糖 mchc ha1c血小板計數。 一個用戶只能在用戶表中有一條記錄,但在篩選表中可能有多條記錄。我想要做的是檢查屬於同一用戶的用戶的多個篩選記錄,看看這些記錄的組合是否提供了我需要的必要信息。如果否,則選擇用戶。例如,必要的信息包括膽固醇,ldl,甘油三酯,葡萄糖或。如果用戶有兩個篩選記錄,一個記錄提供膽固醇(NOT NULL),另一個提供甘油三酯(NOT NULL),葡萄糖(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, 
     sall.id AS screening_id, sall.date AS screening_date, sall.maileddate AS  screening_maileddate 
    FROM users 
    INNER JOIN clients 
    ON(
     users.client_id = clients.id 
     AND users.client_id = '1879' 
     ) 
    INNER JOIN hra 
    ON(
     users.id = hra.user_id 
     AND hra.date BETWEEN '2011-07-01' AND '2011-11-15' 
     AND hra.maileddate IS NOT NULL 
     ) 
    INNER JOIN screening sall 
    ON(
    sall.user_id = users.id 
    ) 
     INNER JOIN screening s1 
     ON(
     s1.user_id = users.id 

     AND s1.date BETWEEN '2011-05-15' AND '2011-11-15' 
    ) 
    INNER JOIN screening s2 
    ON(
     s2.user_id = users.id 

     AND s2.date BETWEEN '2011-05-15' AND '2011-11-15' 
    ) 
    INNER JOIN screening s3 
    ON(
     s3.user_id = users.id 

     AND s3.date BETWEEN '2011-05-15' AND '2011-11-15' 
    ) 
    INNER JOIN screening s4 
    ON(
     s4.user_id = users.id 

     AND s4.date BETWEEN '2011-05-15' AND '2011-11-15' 
    ) 
    INNER JOIN screening s5 
    ON(
     s5.user_id = users.id 

     AND s5.date BETWEEN '2011-05-15' AND '2011-11-15' 
) 
    INNER JOIN screening s6 
    ON(
     s6.user_id = users.id 

     AND s6.date BETWEEN '2011-05-15' AND '2011-11-15' 
) 

    WHERE ((s1.cholesterol IS NULL 
    OR s2.ldl IS NULL 
    OR s3.triglycerides IS NULL) 
    OR (s4.glucose IS NULL 
    AND s5.ha1c IS NULL)) 
    AND s6.maileddate IS NULL 
    GROUP BY users.id 
+0

可能重複[如何選擇多行(MySQL的)組合(http://stackoverflow.com/questions/ 7341090/how-to-select-the-combination-of-multiple-rowsmysql) –

回答

1

你不想內連接,你想要左連接。當任何連接條件失敗時,內部聯接會抑制結果行,而左側聯接允許結果行顯示爲空項。

你離這裏很近。

+0

即使使用INNER JOIN,它仍顯示一些不應顯示的結果 –

1

你真的必須做所有內部聯接的篩選嗎?考慮這個(這不是一個完整的查詢,但建議不同的篩選方法:)

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, 
    sall.id AS screening_id, sall.date AS screening_date, sall.maileddate AS  screening_maileddate 
FROM users 
LEFT JOIN clients 
ON(
    users.client_id = clients.id 
    AND users.client_id = '1879' 
) 
LEFT JOIN hra 
ON(
    users.id = hra.user_id 
    AND hra.date BETWEEN '2011-07-01' AND '2011-11-15' 
    AND hra.maileddate IS NOT NULL 
) 
LEFT JOIN screening sall 
ON(
    sall.user_id = users.id 
) 
WHERE users.id NOT IN 
(
    SELECT user_id FROM screening1 
    WHERE user_id = users.id AND 
     colesterol IS NULL 
     date BETWEEN '2011-05-15' AND '2011-11-15' 
) 
OR users.id NOT IN 
(
    SELECT user_id FROM screening2 
    WHERE user_id = user.id AND 
     ldl IS NULL 
     date BETWEEN '2011-05-15' AND '2011-11-15' 
) 
...