You want to aggregate the table to get one row per ID (GROUP BY id
). You only want those IDs that exist in particalar years and not in other particular years.
Conditions on aggregations are placed in HAVING
. In MySQL true equals 1 and false equals 0, so you can just add them up:
select id
from mytable
group by id
having sum(year = 2013) > 0
and sum(year = 2017) > 0
and sum(year = 2015) = 0
and sum(year = 2018) = 0
order by id;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…