· 8 years ago · Jul 24, 2018, 09:16 PM
1drop table if exists status;
2
3create table status
4(
5status_id tinyint unsigned not null primary key,
6name varchar(32) not null
7)engine=innodb;
8
9insert into status values(1,'status a'),(2,'status b'),(3,'status c');
10
11drop table if exists profile;
12
13create table profile
14(
15profile_id int unsigned not null auto_increment primary key,
16name varchar(32) not null
17)engine=innodb;
18
19insert into profile (name) values ('profile 1'),('profile 2'),('profile 3'),('profile 4'),('profile 5');
20
21drop table if exists profile_status;
22
23create table profile_status
24(
25profile_id int unsigned not null,
26status_id tinyint unsigned not null,
27primary key (profile_id, status_id)
28)engine=innodb;
29
30insert into profile_status values
31(1,1),(1,3),
32(2,1),(2,2),(2,3),
33(3,1),
34(4,1),(4,3),
35(5,3);
36
37select * from status;
38select * from profile;
39select * from profile_status;
40
41select
42 p.*,
43 s.name
44from
45 profile p
46inner join profile_status ps on p.profile_id = ps.profile_id
47inner join status s on ps.status_id = s.status_id
48where
49 s.status_id in (1,3); -- whatever status_ids you want