MySQL连接和连接行而不重复输入

时间:2016-08-25 13:43:25

标签: mysql sql join concatenation

我有一个关于如何将多行输出合并到一行而没有多次使用相同条目的问题。 基本设置是4个表:

  • 约会
  • 用户
  • 动作

和2个中间表:

  • actions_appointments
  • users_appointments

我在帖子的末尾发布了表格的确切结构。

可以对房间表(n:1)中的一个条目进行多次约会,但是一个或多个用户可以加入约会(n:n),并且可以执行一个或多个操作(n:n)。 问题是,我不知道如何为每个用户输出单个约会,并且每个约会仅显示一次操作。

通过这篇文章底部的示例表,我基本上希望这是我的输出:

|----------------|-----------|---------------------|-------------|------------------------|
| appointment_id | room_name |      datetime       |   actions   |     userfullnames      |
|----------------|-----------|---------------------|-------------|------------------------|
|        1       | Studio    | 2016-09-01 15:30:00 | work, sleep | John Doe, Martin Smith |
|        2       | Office    | 2017-04-02 13:00:00 | sleep       | John Doe               |
|----------------|-----------|---------------------|-------------|------------------------|

但是我想出了队列,我得到了这个:

|----------------|-----------|---------------------|-------------|------------------------|
| appointment_id | room_name |      datetime       |   actions   |     userfullnames      |
|----------------|-----------|---------------------|-------------|------------------------|
|        1       | Studio    | 2016-09-01 15:30:00 | work, sleep,| John Doe, Martin Smith,|
|                |           |                     | work, sleep | John Doe, Martin Smith |
|        2       | Office    | 2017-04-02 13:00:00 | sleep       | John Doe               |
|----------------|-----------|---------------------|-------------|------------------------|

我的意思是我有点搞砸了我的连接,但我现在完全被困住了。任何提示?我觉得解决方案很简单,但我现在完全失明了。

我的队列:

SELECT 
    appointments.id AS 'appointment_id', 
    room.name AS 'room_name', 
    appointments.datetime, 
    GROUP_CONCAT(actions.name SEPARATOR ', ') AS 'actions',
    GROUP_CONCAT(users.givenname, ' ', users.surname SEPARATOR ', ') AS 'userfullnames'

FROM appointments
INNER JOIN actions_appointments
        ON appointments.id = actions_appointments.appointments_id
INNER JOIN actions
        ON actions_appointments.actions_id = actions.id
INNER JOIN users_appointments
    ON users_appointments.appointments_id = appointments.id
INNER JOIN users
    ON users_appointments.users_id = users.id
INNER JOIN room
    ON appointments.room_id = room.id
GROUP BY
    appointments.id;

表格结构:

基本表格:

|-------------------|
|   room            |
|-------------------|
|   id   | name     |
|--------|----------|
|    1   | Office   |
|    2   | Studio   |
|-------------------|


|----------------------------------------|
|   appointments                         |
|--------|---------|---------------------|
|   id   | room_id |      datetime       |
|--------|---------|---------------------|
|    1   |    2    | 2016-09-01 15:30:00 |
|    2   |    1    | 2017-04-02 13:00:00 |
|--------|---------|---------------------|


|-----------------------------------------|
|   users                                 |
|-----------------------------------------|
|   id   | username | givenname | surname |
|--------|----------|-----------|---------|
|    1   | j.doe    | John      | Doe     |
|    2   | m.smith  | Martin    | Smith   |
|--------|----------|-----------|---------|


|--------------------|
|   actions          |
|--------------------|
|   id   | name      |
|--------|-----------|
|    1   | work      |
|    2   | sleep     |
|--------------------|

中间表:

|------------------------------|
|   actions_appointments       |
|------------------------------|
| actions_id | appointments_id |
|------------|-----------------|
|        1   |             1   |
|        2   |             1   |
|        2   |             2   |
|------------|-----------------|


|----------------------------|
|   users_appointments       |
|----------------------------|
| users_id | appointments_id |
|----------|-----------------|
|      1   |        1        |
|      2   |        1        |
|      1   |        2        |
|----------|-----------------|

编辑:使用DISTINCT的正确队列 感谢Juan.Queiroz和Mike!

SELECT 
    appointments.id AS 'appointment_id', 
    room.name AS 'room_name', 
    appointments.datetime, 
    GROUP_CONCAT(DISTINCT actions.name SEPARATOR ', ') AS 'actions',
    GROUP_CONCAT(DISTINCT users.givenname, ' ', users.surname SEPARATOR ', ') AS 'userfullnames'

FROM appointments
INNER JOIN actions_appointments
        ON appointments.id = actions_appointments.appointments_id
INNER JOIN actions
        ON actions_appointments.actions_id = actions.id
INNER JOIN users_appointments
    ON users_appointments.appointments_id = appointments.id
INNER JOIN users
    ON users_appointments.users_id = users.id
INNER JOIN room
    ON appointments.room_id = room.id
GROUP BY
    appointments.id;

1 个答案:

答案 0 :(得分:0)

GROUP BY
appointments.id, 
room.name, 
appointments.datetime