[thelist] MySQL 5 Query

Fred Jones fredthejonester at gmail.com
Thu Jul 3 06:26:51 CDT 2008


With this simple SQL:

select users.mail, profile_values.value, profile_values.fid
from users
left join profile_values on users.uid=profile_values.uid
where users.uid=1

I get this result set:

"mail"                       "value" "fid"
"fredthejonester at gmail.com" "fred"  "1"
"fredthejonester at gmail.com" "jones" "2"

because there are two rows in profile_values which match. What I want,
however, is one row here, not two. So I tried this:

select mail, x.value as value1 , y.value as value2 from
(select users.mail, profile_values.value
from users
left join profile_values on users.uid=profile_values.uid
where users.uid=1 and fid=1) as x ,
(select   profile_values.value
from users
left join profile_values on users.uid=profile_values.uid
where users.uid=1 and fid=2) as y

and it works. I get:

"mail"                       "value1" "value2"
"fredthejonester at gmail.com" "fred"    "jones"

which is perfect--one row with all the data I want.

Is this the correct and/or intelligent to go about this? Or is there a
better way to do this in MySQL 5?

Thanks.



More information about the thelist mailing list