SQL to Get Data Other User Data from Same Group - php

I'm having a hard time wrapping my head around the SQL I need to pull data about other users that belong to the same group as the logged in user in my PHP application.
The data structure looks like this:
User Table
user_id
name
status
bio
Group Table
group_name
user_id
What I'm trying to do is create a page that shows the bios for all of the other members of the same group or groups that a user belongs to.
I've tried this SQL:
SELECT list.listname, list.user_id, list.groupname
FROM list
LEFT JOIN user_group ON list.user_id = user_group.user_id
WHERE user_group.user_id = 'test#testuser.com'
ORDER BY list.groupname
But I just get back the bio for test#testuser.com, and no other bios. If I remove the WHERE portion of the statement, I get all bios for all users, and not just the bios of users that are in the same group as my test#testuser.com. The logged in user may belong to more than one group.
Any ideas about how to grab this data?

This returns the groups for a given user:
SELECT l.groupname
FROM list l
WHERE l.user_id = 'test#testuser.com'
ORDER BY l.groupname;
If you want all users in the groups
SELECT l.listname, l.user_id, l.groupname
FROM list l LEFT JOIN
user_group ug
ON l.user_id = ug.user_id
WHERE l.groupname IN (SELECT l2.groupname
FROM list l2
WHERE l2.user_id = 'test#testuser.com'
)
ORDER BY l.groupname;

You can do it by self-joining the list table:
SELECT l.listname, l.user_id, l.groupname
FROM list l
LEFT JOIN user_group ug ON l.user_id = ug.user_id
INNER JOIN list l2 ON l2.groupname = l.groupname
AND l2.user_id = 'test#testuser.com'
ORDER BY l.groupname;

Related

SQL get all users activity summed in a query

I want get all users from users table and have a column summed up on all articles length written by all users from article table
Basically
Here is a code if I choose to run an SQL query for each loop (not good)
SELECT * FROM users
--loop
SELECT SUM(wordcount) AS totalwordcount FROM articles WHERE writer_id = user_id
I have used LEFT JOIN but I can only get a list of users with their user_id present on article table
SELECT users.name, SUM(article.wordcount) AS xox FROM users LEFT JOIN article ON users.id_usera = article.writer_id WHERE article.editor_status = 'finished' ORDER BY users.id_usera
With this query, I only get users with their user_id present in the article table but I want all users listed and NULL/empty returned if the user has no article with his/her id present
You need GROUP BY:
SELECT u.name, SUM(a.wordcount) AS xox
FROM users u LEFT JOIN
article a
ON u.id_usera = a.writer_id AND
a.editor_status = 'finished'
GROUP BY u.name;
SELECT users.name, SUM(article.wordcount) AS TotalWordCount
FROM users
LEFT JOIN article ON users.id_usera = article.writer_id
WHERE article.editor_status = 'finished'
GROUP BY article.writer_id
ORDER BY users.id_usera

MySQL: Accounting for NULL or 0 values when multiple joins exist on one table

I have a query that looks up people's full names based on their record ID's in a table called users. The full names are tied to their roles in another table (table1). This requires multiple joins to the users table:
SELECT table1.id, users.full_name AS "Requester",
users.full_name AS "Approver,"
users.full_name AS "Ordered By",
users.full_name AS "Received By"
FROM table1
JOIN users AS users
ON table1.requester_id = users.id
JOIN users AS users2
ON table1.approver_id = users2.id
JOIN users AS users3
ON table1.ordered_by = users3.id
JOIN users AS users4
ON table1.received_by = users4.id
WHERE table1.deleted_record !=1;
The problem I'm having is with ordered_by and received_by. Often, they don't yet exist, because the order has neither been ordered nor received, so the ID for each can be 0, which has no corresponding value in the userstable. When I run this query, I should get back all 475 records that exist, but I only get back 365, because of those 0 values. How can I modify this query to make sure all rows are returned, even if ordered_by and/or received_by = 0?
First, your primary table driving the query should be table1. Then, you are using JOIN instead of LEFT JOIN. LEFT JOIN will give you a null result if no link, but not fail. In which case, you might have to use an IF for your fields value
SELECT table1.id, req.full_name AS "Requester",
app.full_name AS "Approver",
ordr.full_name AS "Ordered By",
rec.full_name AS "Received By"
FROM table1
LEFT JOIN users AS req
ON table1.requester_id = req.id
LEFT JOIN users AS app
ON table1.approver_id = app.id
LEFT JOIN users AS ordr
ON table1.ordered_by = ordr.id
LEFT JOIN users AS rec
ON table1.received_by = rec.id
WHERE table1.deleted_record !=1;
This should do it
You are looking for left join:
SELECT t1.id, ur.full_name AS "Requester",
ua.full_name AS "Approver,"
uo.full_name AS "Ordered By",
urv.uo AS "Received By"
FROM table1 t1 LEFT JOIN
users ur
ON t1.requester_id = ur.id LEFT JOIN
users ua
ON t1.approver_id = ua.id LEFT JOIN
users uo
ON t1.ordered_by = uo.id LEFT JOIN
users urv
ON t1.received_by = urv.id
WHERE t1.deleted_record <> 1;
Note that I changed the aliases on the users references from fairly meaningless u1, u2, etc. to ua, uo, and so on. Also, these need to be used in the SELECT to get the right full name.

Many-to-Many with WHERE (PHP and MySQL)

I have a many-to-many table which is named with "users_groups". With this table I want to assign the unique user_id to a group_id. This works pretty well but now I want to make a panel which shows in which groups the current user is and what users are in these groups.
This is my current code for getting all groups with the users of them.
MySQL-Part
$all_groups = mysqli_query
($db,
"SELECT group_name,GROUP_CONCAT(realname SEPARATOR ', ') AS users
FROM groups
JOIN users_groups ON groups.group_id=users_groups.group_id
JOIN users ON users_groups.user_id=users.user_id
GROUP BY group_name
"
);
PHP-Part
echo'
<table class="table_standard">
<tr>
<th class="th_titlebar">Group</th>
<th class="th_titlebar">Members</th>
</tr>';
while($row_all_groups = mysqli_fetch_array($all_groups)) {
echo '<tr>';
echo '<td class="td_contentbar">'.$row_all_groups["group_name"].'</td>';
echo '<td class="td_contentbar">'.$row_all_groups["users"].'</td>';
echo '</tr>';
}
echo '</table>';
And now I have no idea how to include the WHERE-Part in the MySQL. I tried it already with WHERE users_groups.user_id = $session_user_id but with this method the member lists of the groups were just filled with the current user.
My next idea was to make a first MySQL-Request like SELECT group_id FROM users_groups WHERE user_id=$user_id which safes the group_id's in an array and so on but this did not work for me, no idea why.
You can use a sub select and an additional join for your junction table to get only groups #session_user_id has an association and in GROUP_CONCAT part get all members from the resultant groups
SELECT g.group_name,GROUP_CONCAT(DISTINCT u.realname SEPARATOR ', ') AS users
FROM groups g
JOIN (SELECT * FROM users_groups WHERE user_id =#session_user_id) ug
ON g.group_id=ug.group_id
JOIN users_groups ug1 ON g.group_id=ug1.group_id
JOIN users u ON ug1.user_id=u.user_id
GROUP BY g.group_name
DEMO
Are you trying to get it show what usergroup the current logged in user is in?
If so then this would be your sql based on the above:
SELECT group_name,GROUP_CONCAT(realname SEPARATOR ', ') AS users
FROM users
JOIN users_groups ON users_groups.user_id = users.user_id
JOIN groups ON groups.group_id = users_groups.group_id
WHERE users.user_id = $session_user_id
GROUP BY groups.group_id
That should return a result with all the all the groups and the current user. To remove this repeating User if you was to just show the current groups the current user is assigned to then remove the realname part from the select and just have the group_name.
shows in which groups the current user is and what users are in these groups.
the groups the current user is in:
SELECT g.group_name
FROM groups g
JOIN users_groups ug
ON ug.group_id = g.group_id
WHERE ug.user_id = :session_user_id
ORDER BY g.group_name
To get a list of users that are in those groups, we need another join to the users_groups table. We can also add the join to the users table to get the information for those users...
-- og = other user_id that are in a a group
-- ou = info about those other users
SELECT g.group_name
, ou.user_id
, ou.realname
FROM groups g
JOIN users_groups ug
ON ug.group_id = g.group_id
JOIN users_groups og
ON og.group_id = g.group_id
JOIN users ou
ON ou.user_id = og.user_id
WHERE ug.user_id = :session_user_id
ORDER BY g.group_name, ou.user_id
If you want to omit the current user from the list of users, you can add an inequality predicate.
If you want to collapse that to a single row for each group, you can use the GROUP BY clause and the GROUP_CONCAT function, as in your first query. For a more deterministic result, consider adding an ORDER BY in the GROUP_CONCAT. Beware of the group_concat_max_len setting, which limits the size of the string returned.

Fetching user from friends list and follow list

I am working on social networking site.
I've three tables one is user table which is used to store user details, another table is follow table which is used for followers following list.
In this table I am storing user_id and follower_id.
Third table is user_friends in this I'm storing user_id and friend_userid.
I want to search the user from my friends list and follow list. For this i've written query like this:-
select f.follower_id,uf.friend_userid,u.user_id,u.first_name,u.last_name from tbl_user u
LEFT JOIN tbl_userfriends uf ON uf.friend_userid = u.user_id
LEFT JOIN tbl_follow f ON f.follower_id = u.user_id
where uf.friend_userid != '11'
AND u.first_name LIKE '%a%'
This query returning users only who are friends it is not returning the follow users.
Any help will be appreciated. Thanks in Advance.
You're joining tbl_follow on the Follower ID being equal to the User ID. I suspect that's probably not right.
If you don't already have one you'll need a user id key in the follower table to join on, then you can change your join to;
LEFT JOIN tbl_follow f ON f.userid = u.user_id
i've done this by using following query:-
select u.user_id,u.first_name,u.last_name from tbl_user u LEFT JOIN tbl_userfriends uf ON uf.friend_userid = u.user_id LEFT JOIN tbl_follow f1 ON f1.follower_id = u.user_id LEFT JOIN tbl_follow f2 ON f2.user_id = u.user_id where (uf.user_id = '11' OR f1.user_id = '11' OR f2.follower_id = '11') AND (u.first_name LIKE '%s%' OR u.last_name LIKE '%s%') AND u.status = '0' group by u.first_name
This query returning me all the users who are my followers, friends and to whom i am following.

Mysql select table which depends on other two tables

I have three table group, user and belongs where belongs is relation between group and user.
Table group
group_id (pk)
group_name
Table user
user_id (pk)
user_name
Table belongs
user_id
group_id
I want to show a random groups that people in specified group are also in.
For example specified group is where group_id = 1, Of course, i can get people in this group by use
"SELECT user_id FROM user u JOIN belongs b ON u.user_id = b.user_id and b.group_id = '1'"
I have been trying, and now, i have no idea how to select group that these people are in too.
"SELECT g.* FROM group g WHERE ..."
Thanks in advance.
You can get all the groups by using an extra join:
SELECT DISTINCT b2.group_id
FROM user u JOIN
belongs b
ON u.user_id = b.user_id and b.group_id = '1' JOIN
belongs b2
ON b2.user_id = b.user_id;
One way to choose a random such group is to use order by rand() limit 1. You might want to add where b2.group_id <> '1'.
Assuming that you want to count the members of group_id = 1 in these groups, you would just use group by:
SELECT b2.group_id, count(distinct b2.user_id) as numusers
FROM user u JOIN
belongs b
ON u.user_id = b.user_id and b.group_id = '1' JOIN
belongs b2
ON b2.user_id = b.user_id
GROUP BY b2.group_id;
The join to user is superfluous, but I have left it in assuming that it might be used for some other conditions not in the original question.
You can get a list of all the other groups associated with people in group 1 by using a sub-query:
SELECT g.group_name, COUNT(g.group_name)
FROM group g
INNER JOIN belongs b ON b.group_id = g.group_id
WHERE b.user_id IN ( SELECT u.user_id FROM user u JOIN belongs b ON u.user_id = b.user_id and b.group_id = '1' )
GROUP BY g.group_name
ORDER BY COUNT(g.group_name) DESC

Categories