2012-03-17 44 views
0

好吧我有一個Project_Assigned表,其中包含分配給project_id的account_ids列表。不存在:選擇未分配給特定項目的帳戶?

下面我試圖收集當前未分配給特定項目的所有客戶端的列表。

目前下面的查詢如果沒有客戶端與特定項目相關聯,但只要我將一個客戶端分配給項目,結果集將不返回任何內容,而應返回所有未分配給其他客戶端的客戶端該項目。

SELECT Account.Account_ID, 
Client.First_Name AS `First_Name`, Client.Last_Name AS `Last_Name`, Client.Profile_Pic, Client.Job_Title, Client.Company_Name 
FROM Client 
LEFT JOIN Account ON (Account.Account_ID = Client.Client_ID) 
LEFT JOIN `Admin_Agency` ON (Client.Agency_ID_FK = Admin_Agency.Agency_ID) 
WHERE Account.Access_Type = 'Client' && NOT EXISTS 

(SELECT Account.Account_ID, Account.Access_Type 
FROM `Project_Assigned` projects 
INNER JOIN `Account` ON (projects.AccountID = Account.Account_ID) 
INNER JOIN `Project` ON (projects.ProjectID = Project.Project_ID) 
LEFT JOIN `Client` ON (Account.Account_ID = Client.Client_ID) 
WHERE projects.ProjectID = 48 && Account.Access_Type = 'Client'); 

表Project_Assigned:

+-----------+-----------+ 
| ProjectID | AccountID | 
+-----------+-----------+ 
|   4 |   3 | 
|   4 |   4 | 
|   4 |   5 | 
|   4 |   6 | 
|  12 |   1 | 
|  12 |  11 | 
|  12 |  12 | 
|  29 |  18 | 
|  31 |  18 | 
|  46 |  18 | 
|  48 |  18 | 
|  48 |  12 | 
+-----------+-----------+ 

回答

0

這很可能是我誤解你的模式,或你的需求,但它聽起來像是你只是想這樣的:

SELECT Client_ID, First_Name, Last_Name, Profile_Pic, Job_Title, Company_Name 
    FROM Client 
WHERE Client_ID IN 
     (SELECT Account_ID 
      FROM Account 
      WHERE Access_Type = 'Client' 
     ) 
    AND Client_ID NOT IN 
     (SELECT Account_ID 
      FROM Project_Assigned 
      WHERE ProjectID = 48 
     ) 
; 

沒有?

+0

嗨,我做了一個pwew調整編輯上面,現在它的工作。謝謝,這真的有幫助。 – Malcr001 2012-03-17 17:25:30

+0

@ user971824:不客氣! – ruakh 2012-03-17 17:28:34

+0

@ user971824:你的編輯對我來說並不合適。例如,「LEFT JOIN Admin_Agency」的用途是什麼?查詢中沒有其他內容實際上是指「Admin_Agency」表。 – ruakh 2012-03-17 17:30:24