Here’s the problems…
I have 4 tables: users, groups, users_groups, and emails. Note that users_groups references both the groups and users table. In other words, each time a user joins a group, the userId and groupId is entered into the users_groups table. A user can join multiple groups.
CREATE TABLE app.users (
`id` INT NOT NULL AUTO_INCREMENT ,
`firstName` INT NOT NULL ,
`lastName` INT NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
CREATE TABLE app.groups (
`id` INT NOT NULL AUTO_INCREMENT ,
`groupName` VARCHAR(32) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
CREATE TABLE app.users_groups (
`id` INT NOT NULL AUTO_INCREMENT ,
`userId` INT NOT NULL ,
`groupId` INT NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
CREATE TABLE app.emails (
`id` INT NOT NULL AUTO_INCREMENT ,
`userId` INT NOT NULL ,
`email` VARCHAR(32) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
Now I would like to query these tables and display the result like this:
+---------+-------------------+--------------+
|name |emails |groups |
+---------+-------------------+--------------+
|John Doe |johndoe@mail.com, |group1,group2,|
| |johndoe2@mail.com |group3 |
+---------+-------------+---------+----------+
You could use
group_concatfor that: