Mysql select table which depends on other two tables - php

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

Related

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.

Using LEFT JOIN with 2 different query GROUP BY

My first SQL query is:
SELECT username,COUNT(username) as total_active_users FROM users WHERE active = '1' GROUP BY referrer // $act_user
To count the active users for each referrer
The second sql query is:
SELECT COUNT(orders) as total_user_orders FROM users_orders GROUP BY $act_user['username'] // the username from the first query.
To count total number of orders for each user which i got from the first query
What I'm trying to do it using LEFT JOIN to only count active users who have 1 order at least
The problem is: I have 2 different GROUP BY the first one is "referrer" and the second is "username"
I'm trying to do something like :
SELECT u.username, COUNT(u.username) as total_active_users, COUNT(b.orders) as
total_user_orders FROM users u LEFT JOIN users_orders b on u.username = b.username
WHERE u.active = '1' AND total_user_orders >= '1' GROUP BY (u.referrer for u) and (b.username for b)
Any idea please?
You can do this instead:
SELECT
a.username,
COUNT(u.username) as total_active_users,
COUNT(b.orders) as total_user_orders
FROM
(
SELECT username FROM users_orders WHERE total_user_orders >= '1'
UNION ALL
SELECT username FROM users WHERE u.active = '1'
) AS a
LEFT JOIN users AS u ON a.username = u.username
LEFT JOIN users_orders AS b on u.username = b.username
GROUP BY a.username;
Assuming that user names are unique and that for a user to have at least one order means the same as for them to have at least one row in the users_orders table, you could do the following:
SELECT
u.referrer,
COUNT(DISTINCT u.username) AS usercount
FROM users u
INNER JOIN users_orders uo ON u.username = uo.username
WHERE u.active = 1
;
The join between users and users_orders acts as a filter (in addition to that on the active status). It may produce duplicate user names but COUNT(DISTINCT) will count unique entries only.

Select a row from two tables depend on primary key(Mysql)?

I have two table users and album. In users table there is user_id primary key .In other table albums there are multiple rows with that user_id because every time when a user upload a new album it uploads with user_id as foreign key. I want to select only once the user_id with other table(album) ignore other result set.
How can I achieve this?
SELECT a.*, b.*
FROM users a
INNER JOIN album b
ON a.user_ID = b.user_ID
INNER JOIN
(
SELECT user_ID, MAX(photo_id) max_rec
FROM album
GROUP BY user_ID
) c ON b.user_ID = c.user_ID AND
b.photo_id = c.max_rec
SELECT album.* FROM album LEFT JOIN users ON user.id = album.id WHERE user.id = SOMEIDHERE
I believe this will work, your not giving me a whole lot of info to work with.
SELECT *
FROM ( SELECT u.*, a.*
FROM users AS u
INNER JOIN album AS a
ON u.user_ID = a.user_ID
ORDER BY a.created DESC) AS h
GROUP BY user_ID
ORDER BY b.created DESC -> ORDER BY whatever row you wish for. In this case the newest one is chosen.

Newest loan per user for all users

// Edit:
All work fine with:
SELECT u.*,
l.cod AS loans_cod,
l.step AS loans_step
FROM users AS u
LEFT JOIN loans AS l
ON u.id = l.users_id
WHERE l.step < 12
OR NOT EXISTS
(SELECT l.id
FROM loans
WHERE l.users_id = u.id
)
GROUP BY u.id
Now, I can select all user, and his last loan. Thanks, and I think my solution will help future users.
if you want the last loan, you could append something like this
ORDER BY l.date DESC GROUP BY u.id
SELECT u.*,
l.cod AS loans_cod,
l.step AS loans_step
FROM users AS u
LEFT JOIN loans AS l
ON u.id = l.users_id
WHERE u.id != :id
AND l.timestamp = (SELECT MAX(l.timestamp)
FROM loans AS l2
WHERE l2.users_id = l.users_id)
Where timestamp is a column on your table that indicates when the loan was created or otherwise gives you a time that you can compare to.
Try this:
SELECT u.*, l.cod AS loans_cod,
l.step AS loans_step
FROM users AS u inner join
(
select max(l.loan_id) as loan_id, l.users_id , l.cod, l.step
from loans l
group by
l.users_id,l.cod,l.step
) as l
ON u.id = l.users_id
WHERE u.id != :id
Note: Avoid doing select * it's a very bad practice. List all the columns you need only.

php friends table

I'm struggling with this for hours so please help me.
This is my users table
id | username | last_activity(timestamp for online system)
And this is my friends table
id | uid | fid
What I want is to order the output by last_activity
My current query looks like this:
SELECT fid FROM friends WHERE uid='$user_id'
SELECT f.fid FROM f.friends
LEFT JOIN users u ON f.uid = u.id
WHERE uid=$user_id
ORDER BY u.last_activity DESC
You want to use INNER JOIN to join the two tables:
SELECT f.fid
FROM friends f
INNER JOIN users u
ON u.id = f.id
ORDER BY u.last_activity DESC
Always make sure you type out a real JOIN clause as some ways are old and getting more and more unsupported.
Read more here:
INNER JOIN on w3schools
I think you want
SELECT f.fid AS fid
FROM friends f, users u
WHERE f.uid = u.id AND f.uid = $user_id
ORDER BY u.last_activity DESC
I assume your problem is that fid is on one table, but the ordering criterion last_activity is on another table. The goal of this query is to JOIN each row in the friends table with the corresponding row in the users table (via the WHERE clause).
Assuming uid in friends table is foreign key references to id in users
table. so using INNER JOIN you can retrieve your desired results.
SELECT f.fid
FROM friends f INNER JOIN user_tb u
ON u.id = f.uid
WHERE f.uid = '$user_id'
ORDER BY u.last_activity DESC;

Categories