2016-04-26 128 views
1

我試圖返回salesperson_id和salesperson_lnames列表,其中salesperson_id沒有FL或HI中的客戶。訪問SQL NOT LIKE查詢

我當前的查詢是

SELECT DISTINCT s.salesperson_id, s.salesperson_lname 
FROM salesperson_t AS s inner join customer_t AS c on s.salesperson_id=c.salesperson_id 
WHERE c.state not in ('HI', 'FL'); 

而且它無論是HI或F11返回僅排除salesperson_id和salesperson_lname如果他們有客戶在這兩個HI和佛羅里達州,而不是havinga單一客戶名單。

Relevant TableCurrent Output

回答

0

如果我理解正確的,你想只返回那些銷售人員沒有客戶Hi或FL(或兩者)。如果是這樣,您可以使用having來檢查一個組是否有HI或FL中的任何客戶,如果他們這樣做,則排除他們:

SELECT s.salesperson_id, s.salesperson_lname 
FROM salesperson_t AS s 
INNER JOIN customer_t AS c on s.salesperson_id=c.salesperson_id 
GROUP BY s.salesperson_id, s.salesperson_lname 
HAVING COUNT(CASE WHEN c.state IN ('HI', 'FL') THEN 1 END) = 0