Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9144291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:14:01+00:00 2026-06-17T10:14:01+00:00

I have a table as T1 and table as T2 like the following: T1

  • 0

I have a table as T1 and table as T2 like the following:

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

How to get this result:

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

Or can I get this result in 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...)]
-------------------------------------------------------

Any idea?

EDIT 1

Or can I get the result as the following?

T5 (US of country’s result)

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

T6 (JP of country’s result)

-------------------------------------------------------
id | price   | email
-------------------------------------------------------
1  | $820    | jacky@domain.jp
2  | $2000   | angle@domain.com
3  | $3000   | kevin@domain.com
-------------------------------------------------------
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:14:02+00:00Added an answer on June 17, 2026 at 10:14 am

    This type of data transformation is a pivot. MySQL does not have a pivot function, but you can replicate it using an aggregate function with a CASE expression:

    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
    

    See SQL Fiddle with Demo

    Edit #1, if you just want to use joins instead of the aggregate functions, then your query will be similar to this:

    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'
    

    See SQL Fiddle with Demo

    Result:

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

    Edit #2: To get the result similar to T5 and T6, then you will use the following. For T6 replace the US with JP:

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

    See SQL Fiddle with Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a table named [ProductPriceHistory] like the following: HistoryID..ProductCode..EffectDate.... Price.... IsActive...ProductName 1----------11-----------1
I have a table which looks something like the following... id price condition sell
I have a table like the following: RuleStep StepID | Step Property | Step
I have a table like the following: ID Output ------------------------- 01ABC1 AB 01ABC2 AB
Say I have a database table like the following: FileID | FileName | FileSize
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
In SQL Server 2008 I have a user table like the following: Userid Username
I have a table that looks like the following: <table class=theClass> <tr> <td class=anotherClass><strong>Label1:</strong></td>
i have a table in sqlite database like following. id status ============= x 0
I have a table in MySQL That looks like the following: date |storenum |views

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.