2016-08-25 2 views
0

複数の行出力を同じエントリを複数回使用することなく1つの行にマージする方法について質問があります。MySQLは、繰り返しエントリのない行を結合して連結します

  • 部屋
  • 予定
  • ユーザー
  • アクション

そして2つの中間テーブル: 基本的な設定は、4つのテーブルである

  • actions_appointments
  • users_appointments

投稿の最後にテーブルの構造を投稿します。

ルームテーブル(n:1)の1つのエントリに対して複数のアポイントを作成できますが、1人以上のユーザーがアポイント(n:n)に参加し、1つ以上のアクションを実行できます。 問題は、各ユーザーで1つの予定を出力する方法と予定ごとに1回しか表示されない方法がわからないことです。私は基本的に、これは私の出力になりたい、この記事の下部にある例のテーブルでは

 
|----------------|-----------|---------------------|-------------|------------------------| 
| 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    | 
|----------------|-----------|---------------------|-------------|------------------------| 

I:

 
|----------------|-----------|---------------------|-------------|------------------------| 
| 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    | 
|----------------|-----------|---------------------|-------------|------------------------| 

しかし、私が思いついたキューで、私はこれを取得

私はちょっと私が私の結合を台無しにしたことを意味するが、私は完全に現時点で立ち往生しています。何かヒント?私はソリューションがシンプルだと思っていますが、私は今は完全に盲目です。

マイキュー:

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; 
+0

無効GROUP BY、新しいMySQLバージョンで文句を言わない実行されます。 GROUP BY句が指定されている場合、SELECTリスト内の各列参照は、グループ化列を特定するか、または集合関数の引数でなければなりません。 – jarlh

+1

'GROUP_CONCAT(DISTINCT actions.name SEPARATOR '、 ') ' – Mike

+0

@Mike Oh god私はそれを知っていた、私はDISTINCTを忘れてしまった。あなたは大丈夫です、ありがとう、それはそれを解決しました。ここでは暖かいです。 – Broco

答えて

0
GROUP BY 
appointments.id, 
room.name, 
appointments.datetime 
関連する問題