I have a table which includes 130k+ rows. I want to list most liked users.
I try lots of queries but I didnt get result.
table 1 : "users" / user_id / user_name
table 2 : "likes" / datetime / user_id
$query = mysql_query("SELECT COUNT(*) FROM
(
SELECT DISTINCT a.user_id, b.user_id,b.user_name
FROM dbo.likes AS a
INNER JOIN dbo.users AS b
ON a.user_id= b.user_id
) AS subquery;");
and
while($row = mysql_fetch_array($query))
{
echo $row=['user_name'].'-'.$row=['count(*)'];
}
select user_name, count(user_name)
from users, likes
where users.user_id = likes.user_id
group by user_name
Related
I want to show all those users from Users table, whose id is present in table favorite_group_users as user_id for a user whose favorite_groups.id = favorite_group_users.id.
I used below query but it is returning null.
select users.id as user_id, users.first_name as name,
favorite_groups.id as group_id,
favorite_groups_users.user_id as carrier_id
from users
inner join favorite_groups
on users.id = favorite_groups.user_id
inner join favorite_groups_users
on favorite_groups.id = favorite_groups_users.favorite_group_id
where users.id = 38;
You may try nested select
select
favorite_groups.group_id, users_group.user_id
from
favorite_groups_users ,
(select
favorite_groups_users.favorite_group_id,
users.user_id
from
users, favorite_groups_users
where
users.id = 38 and
users.id = favorite_groups.user_id
) users_group
where
users_group.favorite_group_id=favorite_groups.group_id
How do I get friend list from Friends table with counts of friends of my friends (Count not of my friends)
Friends table"
tbl_users_friends
Field 1: id
Field 2: user_id
Field 3: friend_user_id
and I need the out put as:
A has following friedns:
x (10)
y (2)
z (0)
Above is the list of my friends and in parenthesis contains their friends count.
Thanks
select user_id, count(*) cnt
from Friends
where user_id in
(select friend_user_id
from Friends
where user_id = user_id_of_A)
group by user_id
Try something like this:
select u.user_id, u.name, count(uf1.id) as num_friends
from tbl_users_friends uf
inner join tbl_users u on u.user_id = uf.friend_user_id
left join tbl_users_friends uf1 on uf1.user_id = uf.friend_user_id
where uf.user_id = 1
group by u.user_id, u.name
http://sqlfiddle.com/#!9/10033/1
You need to ajust the users table and column names.
Another solution with a subselect but without group by:
select u.user_id, u.name, (
select count(*)
from tbl_users_friends uf1
where uf1.user_id = uf.friend_user_id
) as num_friends
from tbl_users_friends uf
inner join tbl_users u on u.user_id = uf.friend_user_id
where uf.user_id = 1
i want to retrieve Data form 3 specifies Tables namely
UserDetail(Fname,Lname,User_id),
Movies(Movie_id,MovieName)
UserLikedMovies(User_id,Movie_id)
such that when a user enter a specific Movie_id then Userid Fname Lname form User detail MovieName from Movies,,
Here is what i tried
SELECT UserDetail.FName
FROM
UserDetail UserDetail
INNER JOIN
UserLikedMovies UserLikedMovies
ON
UserDetail.User_id = UserLikedMovies.User_id
INNER JOIN
(
SELECT
Movies.MovieName,
Movies.Movie_id
FROM
Movies Movies
INNER JOIN
UserLikedMovies UserLikedMovies
ON
Movies.Movie_id = UserLikedMovies.Movie_id
INNER JOIN
UserDetail UserDetail
ON
UserLikedMovies.User_id = UserDetail.User_id
WHERE
Movies.Movie_id IN ( Select UserLikedMovies.Movie_id from UserLikedMovies where UserLikedMovies.Movie_id = 4)
) as ABC
ON UserLikedMovies.Movie_id = ABC.Movie_id
AND Movies.Movie_id = ABC.Movie_id
Suppose movie_id is 9, then query is below.
SELECT User.Fname, User.Lname, (SELECT MovieName FROM Movies WHERE Movie_id = 9) AS MovieName
FROM UserDetail User
INNER JOIN UserLikedMovies Like
ON User.User_id = Like.User_id
WHERE Like.Movie_id = 9;
It is pretty simple query:
SELECT ud.`User_id`, ud.`Fname`, ud.`Lname`, m.`MovieName`
FROM `Movies` m
RIGHT JOIN `UserLikedMovies` ulm ON ulm.`Movie_id` = m.`Movie_id`
LEFT JOIN `UserDetail` ud ON ud.`User_id` = ulm.`User_id`
WHERE m.`Movie_id` = 4
I do not know why you are using fearful sub-queries for this simple task.
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.
The following is what I got right now, which does not work properly because it checks one row for two different values.
SELECT users.*
FROM users INNER JOIN roles_users ru ON users.id = ru.user_id
WHERE ru.role_id = 1 AND ru.role_id = 2
I would like to select all users that have two rows in roles_users. The one rows role_id should have one and the second should have role_id two.
So select all users that have two rows in the roles_users where one of them has role_id = 1 and the other has role_id = 2.
The above query selects all users that have one row in roles_users that has first one and then two, that's why I get no results and it does not work. So how can I do this right?
SELECT users.id
FROM users INNER JOIN roles_users ON users.id = roles_users.user_id
WHERE roles_users.role_id IN (1, 2)
GROUP BY users.id
HAVING COUNT(*) = 2
Why not just join in on roles_users twice? Ala:
SELECT users.* FROM users
INNER JOIN roles_users ru1 ON users.id = ru1.role_id AND ru1.role_id = 1
INNER JOIN roles_users ru2 ON users.id = ru2.role_id AND ru2.role_id = 2
You need to get a (distinct) list of the users having the required roles. Try this instead:
SELECT users.{column_list}
FROM users as a
JOIN (SELECT user_id
FROM roles_users
WHERE role_id IN (1, 2)
GROUP BY user_id
HAVING COUNT(DISTINCT role_id) = 2) required_role
ON required_role.user_id = users.id