我试图减去以下两个SQL查询的结果,但我无法弄明白。我尝试了NOT IN
和EXCEPT
,但我认为我做错了。以下是我的疑问:
SELECT DISTINCT first_name, last_name, rating, email, phone
FROM users
WHERE rating !='6';
SELECT DISTINCT first_name, last_name, rating, email, phone
FROM users
JOIN user_transactions
ON users.id=user_transactions.user_id
WHERE rating != '6' AND created_on >= '2014-09-01' AND created_on <= CURDATE();
答案 0 :(得分:0)
看来你在这里真正要做的就是获得评级不等于6的所有行,并且created_on日期是在09/01/2014之前。如果我是对的,你可以在一个查询中完成所有这些:
SELECT DISTINCT first_name, last_name, rating, email, phone
FROM users u
JOIN user_transactions ut
ON u.id = ut.user_id
WHERE rating != 6 AND created_on < '2014-09-01';
我之所以做出这样的假设,是因为你正在获取评级不是6的所有行,并希望摆脱created_on日期在09/01和现在之间的行。这与选择创建日期在09/01之前的所有行不一样吗?
答案 1 :(得分:0)
您可以使用聚合和having
子句执行您想要的操作:
SELECT first_name, last_name, rating, email, phone
FROM users u JOIN
user_transactions ut
ON u.id = ut.user_id
WHERE rating <> '6';
GROUP BY first_name, last_name, rating, email, phone
HAVING SUM(created_on >= '2014-09-01' AND created_on <= CURDATE()) = 0;