我有四张桌子:
1)类别
id | name
------------------
1 | category 1
2 | category 2
3 | category 3
4 | category 4
5 | category 5
6 | category 6
2)国家
id | name
------------------
1 | country 1
2 | country 2
3 | country 3
4 | country 4
5 | country 5
6 | country 6
3)用户
id | name | country_id
-------------------------------
1 | john doe 1 | 1
2 | john doe 2 | 2
3 | john doe 3 | 3
4 | john doe 4 | 4
5 | john doe 5 | 4
6 | john doe 6 | 5
4)users_categories
id | category_id | user_id
------------------------------
1 | 1 |2
2 | 1 |3
3 | 2 |4
我需要知道的是,哪些类别在某个国家/地区没有任何用户。
我的出发点是获得没有用户的类别,但我仍然坚持按国家/地区获取列表。
SELECT * FROM categories WHERE id NOT IN (SELECT id FROM users_categories);
任何帮助?
答案 0 :(得分:0)
我没有测试过,但是这样的事情可能会起作用,因为$ country_id是国家的ID。
#parent {
font-size: 0px; // For eliminating gap between inline-blocks
}
.stack {
display: flex;
}
.flexItem {
flex: 0 1;
}
.nuevo {
height: auto;
background: red;
position: relative;
font-size: 16px;
}
.root {
display: inline-block;
font-size: 16px;
}
答案 1 :(得分:0)
您好,您也可以尝试这个
select categories.*,user.*, country.* from users as user
leftjoin country as country on country.id = user.country_id
leftjoin users_categories as users_categories on users_categories.user_id = user.id
leftjoin categories as categories on categories.id = users_categories.category_id
where user.id is null;