I have a “profiles” table with
- userid
- key
- key_value
obviously userid can have many rows
when a user logs in, I store userdata in a session_var
the query uses 3 tables:
- users
- profiles
- openid
I had this,
$sql = "SELECT op.provider, g . * , gp . *, CONCAT(g.firstname, ' ', g.lastname) AS fullname
FROM openid AS op
INNER JOIN users AS g ON g.userid = op.userid
INNER JOIN profiles AS gp ON gp.userid = op.userid
WHERE op.openid =$openid";
but it returns multiple rows with duplicate data depending how many rows there are in the “profiles” table
This is not what I want. I need all the data in one row if that is possible
What is the most efficient solution? I also need it to store in a php array. How does php handle duplicate keys?
You probably want something like a
distinct:That or you use a
group bywith the columns you want to have data grouped by.Lastly, if you want to return multiple rows of data into a single field (but they are different) you could use a mysql
group_concat()function to do so:Okay, I added some extra rows to my example table like this:
and now a
group_concatreturns this:But you can add a nice placeholder using the
coalesce()function nicely like this:The
coalesce()function looks at either multiple columns or a value you manually enter like I did and returns a great identifier to spot your missing field. It will evaluate left to right for the null values.