1

I'm trying to join two tables but the last one twice. What I've got is this:

Table 1
    id name email
    476 Lars Lyngsoe   [email protected]
    478 Lars2 Lyngsoe2 [email protected]
    495 Lars3 Lyngso3  [email protected]

Table 2
    user_id  profile_key      profile_value
    476     'profile.klasse'  '10A'
    495     'profile.klasse'  '10B'
    476     'profile.phone'   '12345678'
    478     'profile.klasse'  '10A'
    478     'profile.phone'   '23432123'
    495     'profile.phone'   '21212143'

Where id in Table 1 equals user_id in Table 2

I've tried to join and make subqueries but nothing works. What I want to achieve is this:

Table
    id  name           email              class  phone
    476 Lars Lyngsoe   [email protected]     '10A'  '12345678'
    478 Lars2 Lyngsoe2 [email protected]  '10A'  '23432123' 
    495 Lars3 Lyngso3  [email protected]  '10B'  '21212143'

Thank's for your help.

Lars

3
  • Include the queries you've tried in your question. Commented Sep 1, 2015 at 11:18
  • Where did you get the phone number? Commented Sep 1, 2015 at 11:20
  • instead of joining twice invert the join so table 2 is your primary table then inner join for each user id against tbl 1 so each record returns multiple rows then just where clause tbl1.user_id vs the passed in id and it'll return what you want. Commented Sep 1, 2015 at 12:11

4 Answers 4

1

This should work:

SELECT t1.id as id, t1.name, t1.email, t2a.profile_value as class, t2b.profile_value as phone
FROM Table1 as t1
LEFT JOIN Table2 t2a ON t2a.user_id = t1.id AND t2a.profile_key = 'profile.klasse'
LEFT JOIN Table2 t2b ON t2b.user_id = t1.id AND t2b.profile_key = 'profile.phone'
Sign up to request clarification or add additional context in comments.

4 Comments

AFAICR on MySQL, table aliases might need the AS keyword. LEFT JOIN Table2 AS t2a but I'm not sure anymore
You are right I should have used a second look, fixed it
@Eloims your recollection is flawed
Thank's a lot for Your help! What if I only want to show rows where profile_value equals eg '10A' - thanks!
0

What you need is two joins with specific profile_key values:

SELECT t1.id, t1.name, t1.email, t2.profile_value AS class, t3.provile_value AS phone
FROM Table1 t1
LEFT JOIN Table2 t2 ON (t1.id = t2.user_id AND t2.profile_key='profile.klasse')
LEFT JOIN Table2 t3 ON (t1.id = t3.user_id AND t3.profile_key='profile.phone')

Comments

0

I have written a database query for you. I hope it will resolve your problem :

Query

SELECT 
    t1.id,
    t1.`name`,
    t1.email,
    CASE t2.profile_key
        WHEN 'profile.klasse' THEN t2.profile_value
    END AS 'class',
    (SELECT profile_value FROM table2 WHERE profile_key = 'profile.phone' AND user_id = t1.id) AS 'phone'
FROM
    table1 t1
        LEFT JOIN
    table2 t2 ON t1.id = t2.user_id AND t2.profile_key = 'profile.klasse' ORDER BY id;

Click SQL Fiddle

2 Comments

Thank You very much - I will play around with Your suggestion.
What if I want to show rows where profile_value equals '10A'? - Thanks
0

Or, slower but simpler...

SELECT t1.*
     , MAX(CASE WHEN t2.profile_key = 'profile.klasse' THEN t2.profile_value END) klasse
     , MAX(CASE WHEN t2.profile_key = 'profile.phone' THEN t2.profile_value END) phone
  FROM t1
  JOIN t2 
    ON t2.user_id = t1.user_id
 GROUP
    BY t1.user_id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.