选择计数时mySQL查询添加条件

时间:2016-11-23 15:57:18

标签: php mysql

我有数据库,书籍和用户,用户表有book_ids。我想要的是获取每个book_id的用户数量,但除了一个book_id,所有其他book_id我想在计数用户时排除包含4个域的电子邮件。我怎么能这样做。

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

以上是我的查询。这只返回book_id的所有用户数,除了66,但我也想要book_id 66,但没有电子邮件检查。请建议

1 个答案:

答案 0 :(得分: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