select t.user_id, count(1) as c from table as t
where t.gift_id in(1004302, 1004004)
group by t.user_id
having count(1)>1
Efficiency issue, there is no data and it cannot be tested
If (user_id, gift_id) is likely to be duplicated, then (user_id, gift_id) must be deduplicated before calculating simultaneous possession.
select t.user_id, count(1) as c from (
select user_id, gift_id from table group by user_id, gift_id
) as t
where t.gift_id in(1004302, 1004004)
group by t.user_id
having count(1)>1
Check the user whose gift_id is 1004302 and save list1. Check the user whose gift_id is 1004004 and save list2. Find the intersection of the two lists
Efficiency issue, there is no data and it cannot be tested
If (user_id, gift_id) is likely to be duplicated, then (user_id, gift_id) must be deduplicated before calculating simultaneous possession.