2016-11-23 58 views
1

我有數據庫中的書和用戶,用戶表有book_ids。我想要得到的用戶數屬於每個book_id,但除了一個book_id所有其他book_ids我想排除具有4個域的電子郵件時計數用戶。我怎樣才能做到這一點。mySQL查詢在選擇計數時添加條件

SELECT count(id) as user_count ,book_id as booksFROM users WHERE auth= 5 AND (disabled=0 OR disabled IS NULL)AND (valid_to >= NOW() OR valid_to IS NULL) 
AND (email NOT LIKE \"%domain.com\" AND email NOT LIKE \"%abc.co\" AND book_id != 66) GROUP BY book_id 

上面是我的查詢。這隻返回除了66之外的book_id的所有用戶數,但是我還希望爲book_id 66計數,但是沒有電子郵件檢查。請指教

回答

1

繼續與你同OR使用的邏輯:

SELECT count(id) as user_count, book_id as books 
FROM users 
    WHERE auth = 5 
    AND (disabled = 0 OR disabled IS NULL) 
    AND (valid_to >= NOW() OR valid_to IS NULL) 
    AND ((email NOT LIKE "%domain.com\" AND email NOT LIKE "%abc.co\") OR book_id = 66) 
GROUP BY book_id 
+0

((電子郵件NOT LIKE 「%domain.com \」 及email NOT LIKE 「%abc.co \」)此檢查僅需要book_id!= 66,如果book_id = 66電子郵件檢查沒有必要,是否可以使用這個? –

+0

是@tharuerera – sagi

+0

它的工作原理。謝謝 –