开发者

Help finishing MySQL Query?

I have two tables

users - id - name - email

users_grou开发者_如何学运维ps - user_id - group_id

There are a couple more fields but these are the ones I am trying to grab.

I am trying to return 'id, name, email, group_id'. I think I have the first part of the query right, I just don't understand how do the WHERE statement. Could someone show me the way please?

"SELECT users.name, users.email, users.id, users_group.group_id FROM users, users_group WHERE id='$user_id'"


You want to use a JOIN statement here. Under the hood that's what your query does already, but actually writing one out is much clearer.

SELECT u.name, u.email, u.id, ug.group_id
FROM users u
INNER JOIN users_groups ug ON ug.user_id = u.id
WHERE u.id = $user_id

(I'm assuming $user_id has properly been escaped previously.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜