im尝试查询匹配所有条件的行数。我的查询就像只针对单个条件条件那样工作。我想知道我是否应该重新整理缺少的东西或整个查询。
我有类似(仅与问题相关的列)的表:
users: categories:
╔═══════╦═══════════╗╔═══════╦═══════════╗
║ id ║ user_type ║║ id ║ name ║
╠═══════╬═══════════╣╠═══════╬═══════════╣
║ id 1 ║ tutor ║║ id 1 ║ name1 ║
║ id 2 ║ other ║║ id 2 ║ name2 ║
╚═══════╩═══════════╝╚═══════╩═══════════╝
ads:
╔════════════╦══════════════╗
║ creator_id ║ category_id ║
╠════════════╬══════════════╣
║ user.id1 ║ category.id1 ║
║ user.id2 ║ category.id2 ║
╚════════════╩══════════════╝
并查询:
SELECT COUNT(DISTINCT users.id) AS count FROM users
INNER JOIN ratings
ON users.id = receiver_id
LEFT JOIN ads
ON users.id = ads.creator_id
LEFT JOIN categories
ON categories.id = ads.category_id
WHERE user_type = "tutor"
AND categories.name IN ("economy","anatomy")
我得到的是
count = 13 (这是“经济”的匹配行数(10)+“解剖学”的匹配行数(5)-两种情况都匹配的行(2), 10 + 5-2 = 13 )
对于条件中的单个值,例如:
AND categories.name IN ("economy")
我得到了适当的结果
我需要什么
count = 2 (这是符合所有条件的总行数)