SELECT a.*,
b.*,
b.id AS user_id,
a.i开发者_开发百科d AS group_id
FROM groups a,
users b
WHERE a.group_leader_id = b.id
Thank you.
Show all groups and only those users who are a leader of a group
SELECT a.*, b.*,
b.id AS user_id, -- use an alias for users.id
a.id AS group_id -- use an alias for groups.id
FROM groups a, -- alias table groups as 'a'
users b -- alias table users as 'b'
WHERE a.group_leader_id = b.id -- show only those users who are a leader of a group, and link them to that group
According to me, your query can be optimized to this:
SELECT a.*, b.*, b.id AS user_id, a.id AS group_id
FROM groups a
INNER JOIN users b ON a.group_leader_id = b.id
SELECT a.*, b.*, b.id //select all columns from a, b and id from table b with alias user_id
AS user_id, a.id // id from table a with alias group_id
AS group_id
FROM groups a, users b //from tables groups and users with aliases a and b
WHERE a.group_leader_id = b.id //where group_leader_id from groups = id from users.
This statement is a join between two tables groups and users on the group leader foreign key of groups.
So I will state it this way: Display the user information of the group leaders (with a redundant renaming of user_id and group_id).
List all groups and their leaders.
SELECT
a.*,
b.*,
b.id AS user_id,
a.id AS group_id
FROM
groups a
INNER JOIN
users b
ON
a.group_leader_id = b.id
It means pick out Group Leaders(doesn't say what details) from 2 tables where B has the list of Group Leaders, and A has the user's details.
Give me all columns from group + all columns from users + rename b.id as user_id + rename a.id as group_id from all rows in groups where there is a match from groups to users.
精彩评论