Skip to content

Instantly share code, notes, and snippets.

@owenconti
Created July 5, 2024 04:04
Show Gist options
  • Save owenconti/ec53fe81b368929e307757d457dd26d2 to your computer and use it in GitHub Desktop.
Save owenconti/ec53fe81b368929e307757d457dd26d2 to your computer and use it in GitHub Desktop.
MySQL 8.2 Intersection not working
create table users (id int);
create table posts (id int, user_id int);
create table articles (id int, user_id int);
insert into users values (1), (2), (3), (4); -- Creating 4 users
insert into posts values (1, 2); -- Creating posts for user_id=2 ONLY
insert into articles values (1, 2); -- Creating articles for user_id=2 ONLY
select
id as 'user_id',
(
select count(*) from (
select user_id from posts where posts.user_id = users.id
INTERSECT
select user_id from articles where articles.user_id = users.id
) as tmp
) as "intersect"
from users;
/*
Expected result:
user_id intersect
1 0
2 1
3 0
4 0
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment