我有以下查询:
select * from members,subscriptions
where members.MemberID = subscriptions.MemberID
and subscriptions.Year = 2009
and members.ASSCID = 15
and subscriptions.Untildate between '$2009-01-01' and '2009-12-31'
order by members.Memberlastname
会员支付其年度订阅费用一次(年度订阅),或者支付2次,一月份一次,六月份一次(六个月订阅).我想要的是拉至少支付一次的会员.
上面的声明会带来一些成员两次(那些在1月和6月均缴费的成员).
可以拉至少支付一次的会员(无论他们是支付年度订阅还是六个月订阅).避免重复.
解决方法:
成员表中的所有记录,其中每个成员至少具有一个订阅:
select members.*
from members join
(
select
members.MemberID, count(*)
from members join subscriptions on (members.MemberID = subscriptions.MemberID)
where subscriptions.Year = 2009
and subscriptions.Untildate between '2009-01-01' and '2009-12-31'
group by members.MemberID
having count(*) >= 1
)
v on ( members.MemberID = v.MemberID)
where members.ASSCID = 15
标签:unique,mysql
来源: https://codeday.me/bug/20191024/1919505.html