Basically, I have two tables (copied from WordPress): users and user_meta. User_meta has 4 columns: meta_id, user_id, meta_key, meta_value. Each row has an unique meta_id, but multiple rows can have the same user_id. I want to get everything from users (users.*) and all of the meta_values associated with the user with as few queries as possible. Also, I want to be able to use mysql_fetch_object on the returned resource.
Here's what I'm using right now:
SELECT *, (SELECT role FROM usermeta WHERE user_id=ID) as role FROM users WHERE ID=1
EDIT: I don't think I was clear that there are multiple usermeta rows with the same user_id and meta_key.
what about join ?
SELECT * FROM users
JOIN user_meta ON users.user_id=user_meta.user_id
WHERE users.user_id=1
Fetch Object:
$q = mysql_query('select * from users join user_meta on users.user_id=user_meta.user_id where users.user_id=1');
while($r = mysql_fetch_object($q))
{
echo $r->user_id;
echo ...
echo ...
}
Try:
select u.* from users u join user_meta um on (u.user_id=um.user_id) where u.id = 1