2017-01-23 59 views
-1

我有一個工作的MySQL查詢被「經理只有」從我向tblUsers:MySQL查詢顯示不是經理的員工?

SELECT COUNT(e.username) AS employeeCount, 
    m.username    AS reports_to, 
    m.id 
FROM tblusers e 
INNER JOIN tblusers m 
ON e.reportsToID = m.ID 
GROUP BY m.username 

但我需要另一個查詢只是讓「員工只有」?

+1

這裏不多說下去。請將樣本數據與預期輸出一起發佈。 – GurV

+1

對於不是員工的人,「reportsToID」的值是多少? – leeyuiwah

+1

這個怎麼樣 '從tblusers中選擇用戶名,其中id不在(從tblusers中選擇reportsToID)' – crowchirp

回答

1

謝謝所有人,尤其是@crai

您的查詢工作得很好:

SELECT username, id, reportstoID 
FROM tblusers 
WHERE id NOT IN (SELECT reportsToID FROM tblusers) AND reportstoID <> '0' 
ORDER BY username 
0
SELECT 
    username 
    , id 
    , reportsToID 
FROM 
    tblusers e 
WHERE 
    e.reportsToID<>0 
ORDER BY username