mysql - LEFT JOINs with multiple tables -
i have following tables:
table users - primary key (user_id) +---------+----------+-----------+ | user_id | username | realname | +---------+----------+-----------+ | 1 | peterpan | peter pan | | 2 | bobfred | bod fred | | 3 | sallybe | sally | | 6 | petersep | peter sep | +---------+----------+-----------+ table users_groups - primary key (user_id, group_id) +---------+----------+ | user_id | group_id | +---------+----------+ | 1 | 1 | | 1 | 2 | | 2 | 1 | | 2 | 2 | | 3 | 6 | | 3 | 9 | | 6 | 6 | | 6 | 9 | +---------+----------+ table game - primary key (id) +----+-------+ | id | game | +----+-------+ | 1 | game1 | | 2 | game2 | | 6 | game6 | | 9 | game9 | +----+-------+ table groups - primary key(group_id) +----------+--------------+---------------+ | group_id | group_name | group_desc | +----------+--------------+---------------+ | 1 | groupname1 | description1 | | 2 | groupname2 | description2 | +----------+--------------+---------------+ table group_game - primary key(group_id, game_id) +----------+----------+ | group_id | game_id | +----------+----------+ | 1 | 1 | | 1 | 2 | | 2 | 6 | | 2 | 9 | +----------+----------+
i want display (like group list):
+----+------------+--------------+---------------------+--------------+ | id | group name | group desc | group members | group games | +----+------------+--------------+---------------------+--------------+ | 1 | groupname1 | description1 | peter pan, bob fred | game1, game2 | | 2 | groupname2 | description2 | sally be, peter sep | game6, game9 | +----+------------+--------------+---------------------+--------------+
now have query gives me no rows (no error, 0 rows):
select g.group_name, g.group_id, g.group_desc, group_concat(distinct ga.game separator ', ') games, group_concat(distinct u.realname separator ', ') users groups g left join users_groups ug1 on g.group_id=ug1.group_id left join users u on ug1.user_id=u.user_id left join group_game gg on g.group_id=gg.group_id left join game ga on gg.game_id=ga.id group g.group_name
how can solve problem or how can write query?
i want show group list information (like group information, users of groups, games of group).
Comments
Post a Comment