0

次のように、T1としてテーブルとT2としてテーブルがあります。

T1

-------------------------------------------------------
id | price  | email
-------------------------------------------------------
1  | $1000  | jacky@domain.com
2  | $2000  | angle@domain.com
3  | $3000  | kevin@domain.com
-------------------------------------------------------

T2

-------------------------------------------------------
id | master | country | key   | value
-------------------------------------------------------
1  | 1      | US      | price | $399 
2  | 1      | US      | email | jacky/domain.us 
3  | 1      | ES      | price | $550 
4  | 1      | ES      | email | jacky@domain.es 
5  | 1      | JP      | price | $820 
6  | 1      | JP      | email | jacky@domain.jp 
7  | 2      | US      | price | $360 
8  | 2      | US      | email | angle@domain.us 
-------------------------------------------------------

この結果を得る方法:

T3

----------------------------------------------------------------------------------------------------------------------------
id | price  | price_US  | price_ES  | price_JP  | email            | email_US        | email_ES        | email_JP
----------------------------------------------------------------------------------------------------------------------------
1  | $1000  | $399      | $550      | $820      | jacky@domain.com | jacky@domain.us | jacky@domain.es | jacky@domain.jp
1  | $2000  | $360      | NULL      | NULL      | angle@domain.com | angle@domain.us | NULL            | NULL
1  | $3000  | NULL      | NULL      | NULL      | NULL             | NULL            | NULL            | NULL
----------------------------------------------------------------------------------------------------------------------------

または、PHPでこの結果を取得できますか?

T4

-------------------------------------------------------
id | price  | email             | more_info
-------------------------------------------------------
1  | $1000  | jacky@domain.com  | [array (rows...)]
2  | $2000  | angle@domain.com  | [array (rows...)]
3  | $3000  | kevin@domain.com  | [array (rows...)]
-------------------------------------------------------

何か案が?

編集1

または、次のような結果を得ることができますか?

T5(国の結果の米国)

-------------------------------------------------------
id | price  | email
-------------------------------------------------------
1  | $399   | jacky@domain.us
2  | $360   | angle@domain.us
3  | $3000  | kevin@domain.com
-------------------------------------------------------

T6(国の結果のJP)

-------------------------------------------------------
id | price   | email
-------------------------------------------------------
1  | $820    | jacky@domain.jp
2  | $2000   | angle@domain.com
3  | $3000   | kevin@domain.com
-------------------------------------------------------
4

2 に答える 2

1

このタイプのデータ変換はピボットです。MySQLにはピボット関数はありませんが、次のCASE式を使用した集計関数を使用して複製できます。

select t1.id,
  t1.price,
  max(case when t2.country = 'US' and `key` = 'price' then t2.value end) Price_US,
  max(case when t2.country = 'ES' and `key` = 'price' then t2.value end) Price_ES,
  max(case when t2.country = 'JP' and `key` = 'price' then t2.value end) Price_JP,
  t1.email,
  max(case when t2.country = 'US' and `key` = 'email' then t2.value end) Email_US,
  max(case when t2.country = 'ES' and `key` = 'email' then t2.value end) Email_ES,
  max(case when t2.country = 'JP' and `key` = 'email' then t2.value end) Email_JP
from table1 t1
left join table2 t2
  on t1.id = t2.master
group by t1.id, t1.price, t1.email

SQL FiddlewithDemoを参照してください

#1を編集します。集計関数の代わりに結合を使用するだけの場合、クエリは次のようになります。

select t1.id,
  t1.price,
  P_US.value Price_US,
  P_ES.value Price_ES,
  P_JP.value Price_JP,
  t1.email,
  E_US.value Email_US,
  E_ES.value Email_ES,
  E_JP.value Email_JP
from table1 t1
left join table2 P_US
  on t1.id = P_US.master
  and P_US.country = 'US'
  and P_US.`key` = 'price'
left join table2 P_ES
  on t1.id = P_ES.master
  and P_ES.country = 'ES'
  and P_ES.`key` = 'price'
left join table2 P_JP
  on t1.id = P_JP.master
  and P_JP.country = 'JP'
  and P_JP.`key` = 'price'
left join table2 E_US
  on t1.id = E_US.master
  and E_US.country = 'US'
  and E_US.`key` = 'email'
left join table2 E_ES
  on t1.id = E_ES.master
  and E_ES.country = 'ES'
  and E_ES.`key` = 'email'
left join table2 E_JP
  on t1.id = E_JP.master
  and E_JP.country = 'JP'
  and E_JP.`key` = 'email'

SQL FiddlewithDemoを参照してください

結果:

| ID | PRICE | PRICE_US | PRICE_ES | PRICE_JP |            EMAIL |        EMAIL_US |        EMAIL_ES |        EMAIL_JP |
------------------------------------------------------------------------------------------------------------------------
|  1 |  1000 |      399 |      550 |      820 | jacky@domain.com | jacky/domain.us | jacky@domain.es | jacky@domain.jp |
|  2 |  2000 |      360 |   (null) |   (null) | angle@domain.com | angle@domain.us |          (null) |          (null) |
|  3 |  3000 |   (null) |   (null) |   (null) |     kevin@domain |          (null) |          (null) |          (null) |

T5編集#2:とに似た結果を得るにはT6、次を使用します。にT6置き換えるにUSJP

select t1.id,
  max(case when `key` = 'price' then value end) price,
  max(case when `key` = 'email' then value end) email
from table1 t1
left join table2 t2
  on t1.id = t2.master
where t2.country = 'US'
group by t1.id
union all
select t1.id,
  t1.price,
  t1.email
from table1 t1
where not exists (select t.id
                  from table1 t
                  left join table2 t2
                    on t.id = t2.master
                  where t2.country = 'US'
                     and t1.id = t.id);

SQL FiddlewithDemoを参照してください

于 2013-01-15T19:05:10.420 に答える
0

テーブル間の関係を与えてから、もう一度やり直してみてください。

于 2013-01-15T19:15:59.643 に答える