我试图从我的列表中仅选择唯一的电子邮件。这是我的数据库的一个例子
1 john@email.com unpaid newspaper
2 Emily@email.com paid magazine
3 terry@email.com unpaid magazine
4 john@email.com paid magazine
问题是,如果我使用SELECT DISTINCT WHERE payment = paid,john就会出现。当我付款=未付款时,约翰也会出现。我只想选择具有良好付款历史(没有未付款)的客户。我该如何解决这个问题?
答案 0 :(得分:1)
您还可以使用sum
与表达式payment = 'unpaid'
来获取没有unpaid
个条目的电子邮件
select email
from table1
group by email
having (sum(payment = 'unpaid')) = 0
答案 1 :(得分:0)
您可以使用exists
运算符:
SELECT DISTINCT email
FROM mytable a
WHERE NOT EXISTS (SELECT *
FROM mytable b
WHERE a.email = b.email AND b.payment = 'unpaid')
答案 2 :(得分:0)
使用subselect尝试not in
:
select * from `table` where payment='paid' and user not in (select user from `table` where payment='unpaid')