Select where user roles 1 and 2 SQL query - php

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

Related

Mysql join on three tables to get list of unique data

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

Mysql count with join from two tables for one user-id

i want to return follower and following numbers for just one user_id from existing two tables which one is users and the other one is follow:
my code is here
SELECT users.id,
users.name,
COUNT(folow.following),
COUNT(folow.follower)
FROM users
JOIN folow ON folow.following = users.id
WHERE users.id = '$other' LIMIT 10
It's probably simplest to use correlated subqueries here:
SELECT id,
name,
(SELECT COUNT(*) FROM folow WHERE following = users.id) followers,
(SELECT COUNT(*) FROM folow WHERE follower = users.id) following
FROM users
WHERE id = ?

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.

Selected by groups with totals (mySQL)

I've been fiddleing with this about 20 minutes now and though I might get some help here. I have two tables, in a many-to-one relationship. users (one) and events (many). The tables, simplified, look like this:
users
-----------
user_id
group_id
events
-----------
user_id
I'm trying to produce a report that will show the number of users with at least one event, per group. so a result set would hopefully look like:
group_id users_with_at_least_one_event
1 689
2 312
SELECT group_id, count(*) users_with_at_least_one_event
FROM
(
SELECT u.user_id, u.group_id, count(*) as event_count
FROM users u
JOIN events e
ON u.user_id = e.user_id
GROUP BY u.user_id, u.group_id
HAVING count(*) > 1
) sub
GROUP BY group_id
SELECT group_id, COUNT(users.user_id) AS users_with_at_least_one_event,
FROM users, events
WHERE users.user_id = events.user_id
GROUP BY group_id
maybe?
try
select users.group_id, count(distinct users.user_id) as users_with_at_least_one_event
from
users, events
where users.user_id = events.user_id
group by users.group_id
SELECT users.group_id, COUNT(*) FROM users
WHERE users.user_id IN (SELECT events.user_id FROM events)

Selecting multiple rows from multiple tables

I have the following table structure:
- table users -
user_id
user_name
- table groups -
group_id
group_name
- table group_members -
group_id
user_id
Let's also say I have this in the DB:
users:
1:administrator
groups:
1:administrators
2:superusers
3:normal users
group_members:
1:1 (user_id 1 is member of group_id 1)
1:2
1:3
Now, how do I go efficient with selecting all users, and with that, the groups they're member of? Do I have to execute 3 queries selecting all rows from all 3 tables, and fetch it with arrays in PHP, or is there a more effecient way to achieve this?
select u.*, g.group_name
from users u
inner join group_members gm on gm.user_id = u.user_id
inner join groups g on g.group_id = gm.group_id
where u.user_id = 123
This query should work if there are no null fields in group and group_members table
if there are null fields replace inner join with left join
select users.username, group.group_name
from users
inner join group_members on group_members.user_id = users.user_id
inner join groups on group.group_id = group_members.group_id

Categories