MySQL - Join 2 tables multiple columns - mysql

I have 2 Tables. members and pass_delivery
Members Table Structure
id,
first_name,
last_name
Pass Delivery Table Structure
id, member_id, member_admin_id, member_user_id, no_of_passes, status
Relation
member_id, member_admin_id and member_user_id contains the id of member table.
Objective
I want to display a row like below:-
ID of member Table
Name of Member
No. of Passes
Status
Member Admin Name
Member User Name
I have tried with joins but not working.. Please help.

You can use inner join to join the two tables since they have unique id which is the id. So create a query that concatenates the two tables.

Try this out :
Suppose PassDelivery and Members are your table names
select id, first_name,last_name,Information, no_of_passes,status,member_admin_id,member_user_id FROM
Members JOIN PassDelivery ON Members.id=PassDelivery .id;

select m.first_name, m.last_name, p.status, m2.first_name, m2.last_name, m3.first_name, m3.last_name from members m
left outer join pass_delivery p on p.member_id=m.id
left outer join members m2 on m2.id=p.delivery_admin_id
left outer join members m3 on m3.id=p.delivery_boy_id

Related

return data with count from two tables in mysql query

I have two tables in my database; groups and members.
groups has the following columns: id, name, description, owner_id, school_id
members has the following columns: id, user_id, group_id
I want to return all the records in group, on each row it should perform a count on the members table to determine how many members each group has.
so my end result should be like this:
id, name, description, owner_id, school_id, count(this is from the members table)
i keep trying but sometimes when i have only 2 groups but 3 members i get 5 rows returned instead of 2.
The standard way to do this is with COUNT(), a LEFT JOIN and a GROUP BY:
SELECT g.*, COUNT(m.id) as member_count
FROM groups g
LEFT JOIN members m
ON m.group_id = g.id
GROUP BY g.id
For performance, ensure you have an index on members.group_id.

Query two tables with result opposite of join like statement

Here is the simple question to gain some medals :) :
I have three tables:
"contacts" that holds the contacts information where each entry has a unique id.
The second table "groups" where is stores the name for each group and the group id.
The last "contacts_groups" binds each contact to a group, it has just two columns, contact_id and group_id.
The question is how do write in a single statement a MySQL query that will select all contacts that are not assigned to a group. In other words contacts which id is not listed in "contacts_groups" table?
select * from contacts c
left outer join contacts_groups cg on c.id = cg.contact_id
where cg.contact_id is null
SELECT * FROM contacts c
WHERE NOT EXISTS (SELECT * FROM contacts_groups cg
WHERE cg.contact_id = c.contact_id)
Just to be complete, here's another solution:
SELECT * FROM contacts
WHERE contact_id NOT IN (SELECT contact_id FROM contacts_groups)
However, I think MySQL tends to execute the left join or correlated subquery more efficiently.

MySQL Left Join Multiple Rows

I have an interesting challenge... I have two tables, products and users.
products contains 2 columns, user_id and current_bidder, which hold two different IDs from the users table.
I would like to select all columns from products, and the name and rating from the users table for each user_id and current_bidder.
Essentially, I'm trying select columns from two different rows on a joined table, while disambiguating their names.
Any help would be greatly appreciated.
Join to the user table twice, and give each copy a different alias. Something like this:
select p.name, p.weight, owner.name, bidder.name
from product p
join user owner
on ...
join user bidder
on ...
The nice way to avoid ambiguity between columns is to add an ALIAS on it.
SELECT a.*, -- selects all records from products
b.name AS user_name, -- user_name is an alias of users.name (user_id)
c.name AS bidder_name -- user_name is an alias of users.name (current_bidder)
FROM products a
LEFT JOIN users b
ON a.user_id = b.id
LEFT JOIN users c
ON a.current_bidder = c.id
The reason why I used LEFT JOIN is because I assumed that some products has no bidder yet. If INNER JOIN was used, product will never be shown on the result until there's a bidder on it.

Mysql group concat on double join

I have a user table from which I want all values, so I have this query:
SELECT tbl_user.* FROM tbl_user
Now I want one additional column in this result which shows all roles this user has, (or nothing if there are no roles for the user). The role information comes from two additional tables.
The first table contains these two values: userid, roleid
The second table contains roleid and role_name.
So the group concat needs to get all role names based on the roleid's in table1.
I have tried several different ways to do this, but I don't succeed. Either I get only one result with several times the same rolename, or no result at all.
Thanks for your help
Michael
Update: added LEFT JOIN for users with no role.
SELECT
tbl_user.*,
GROUP_CONCAT(role_name) AS roles
FROM
tbl_user LEFT JOIN tbl_roles ON tbl_user.userid = tbl_roles.userid
JOIN tbl_rolenames ON tbl_roles.roleid = tbl_rolenames.roleid
GROUP BY tbl_user.userid
Note that MySQL will permit a GROUP BY on fewer columns than appear in the SELECT list in total, but in other RDBMS you would need to explicitly list out the columns in tbl_user and include them in the GROUP BY, or do an additional self join against tbl_user to get the remaining columns from that table.
Something like:
SELECT
urole.userid,
uall.username,
uall.name,
uall.othercols,
urole.roles
FROM
tbl_user uall JOIN (
SELECT
tbl_user.userid,
GROUP_CONCAT(role_name) AS roles
FROM
tbl_user LEFT JOIN tbl_roles ON tbl_user.userid = tbl_roles.roleid
JOIN tbl_rolenames ON tbl_roles.roleid = tbl_rolenames.roleid
GROUP BY tbl_user.userid
) urole ON uall.userid = urole.userid

MySQL joins and COUNT(*) from another table

I have two tables: groups and group_members.
The groups table contains all the information for each group, such as its ID, title, description, etc.
In the group_members table, it lists all the members who are apart of each group like this:
group_id | user_id
1 | 100
2 | 23
2 | 100
9 | 601
Basically, I want to list THREE groups on a page, and I only want to list groups which have MORE than four members. Inside the <?php while ?> loop, I then want to four members who are apart of that group. I'm having no trouble listing the groups, and listing the members in another internal loop, I just cannot refine the groups so that ONLY those with more than 4 members show.
Does anybody know how to do this? I'm sure it's with MySQL joins.
MySQL use HAVING statement for this tasks.
Your query would look like this:
SELECT g.group_id, COUNT(m.member_id) AS members
FROM groups AS g
LEFT JOIN group_members AS m USING(group_id)
GROUP BY g.group_id
HAVING members > 4
example when references have different names
SELECT g.id, COUNT(m.member_id) AS members
FROM groups AS g
LEFT JOIN group_members AS m ON g.id = m.group_id
GROUP BY g.id
HAVING members > 4
Also, make sure that you set indexes inside your database schema for keys you are using in JOINS as it can affect your site performance.
SELECT DISTINCT groups.id,
(SELECT COUNT(*) FROM group_members
WHERE member_id = groups.id) AS memberCount
FROM groups
Your groups_main table has a key column named id. I believe you can only use the USING syntax for the join if the groups_fans table has a key column with the same name, which it probably does not. So instead, try this:
LEFT JOIN groups_fans AS m ON m.group_id = g.id
Or replace group_id with whatever the appropriate column name is in the groups_fans table.
Maybe I am off the mark here and not understanding the OP but why are you joining tables?
If you have a table with members and this table has a column named "group_id", you can just run a query on the members table to get a count of the members grouped by the group_id.
SELECT group_id, COUNT(*) as membercount
FROM members
GROUP BY group_id
HAVING membercount > 4
This should have the least overhead simply because you are avoiding a join but should still give you what you wanted.
If you want the group details and description etc, then add a join from the members table back to the groups table to retrieve the name would give you the quickest result.