sql - Join tables in sqlite with many to many -
i have following database schema:
create table people ( id integer primary key autoincrement, ); create table groups ( id integer primary key autoincrement, );
and have people members of groups in separate file (let's in tuples of (person id
, group id
). how can structure database schema such it's easy access person's groups, , easy access members of group? difficult , slow read tuples have, want in database form. can't have things member1
, member2
, etc. columns because number of people in group unlimited.
move text file database table
create table groups_people ( groups_id integer, people_id integer, primary key(group_id, people_id) );
and select people member of group 7
select * people p left join groups_people gp on gp.people_id = p.id gp.groups_id = '7';
and select groups person 5 in
select * groups g left join groups_people gp on gp.groups_id = g.id gp.people_id = '5';
Comments
Post a Comment