One table, need multiple values from different rows/tuples
Posted
by WmasterJ
on Stack Overflow
See other posts from Stack Overflow
or by WmasterJ
Published on 2010-06-17T00:30:47Z
Indexed on
2010/06/17
0:42 UTC
Read the original article
Hit count: 386
I have tables like:
'profile_values'
userID | fid | value
-------+---------+-------
1 | 3 | [email protected]
1 | 45 | 203-234-2345
3 | 3 | [email protected]
1 | 45 | 123-456-7890
And:
'users'
userID | name
-------+-------
1 | joe
2 | jane
3 | jake
I want to join them and have one row with two of the values like:
'profile_values'
userID | name | email | phone
-------+-------+----------------+--------------
1 | joe | [email protected] | 203-234-2345
2 | jane | [email protected] | 123-456-7890
I have solved it but it feels clumsy and I want to know if there is a better way to do it. Meaning solutions that are either more readable or faster(optimized) or simply best-practice.
Current solution: multiple tables selected, many conditional statements:
SELECT u.userID AS memberid,
u.name AS first_name,
pv1.value AS fname,
pv2.value as lname
FROM users AS u,
profile_values AS pv1,
profile_values AS pv2,
WHERE u.userID = pv1.userID
AND pv1.fid = 3
AND u.userID = pv2.userID
AND pv2.fid = 45;
Thanks for the help!
© Stack Overflow or respective owner