Selecting multiple rows from multiple tables - php

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

Related

how write sql query with multiple join?

I have fours tables and I wanted to join all three tables with the one table.
I have listed my problem as follows:
Tables:
users
user_training
user_courses
user_certificates
I wanted to get the data from [2,3,4] tables that user_id field matches with the users table ID field.
When I try the INNER JOIN it gives me the result for users that are common in all the tables, But I just wanted to check the [2,3,4] tables with the table [1] Records.
My Query...
SELECT A.training_name AS 'training_name', C.course_name AS 'course_name', D.certificate_name AS 'certificate_name'
FROM user_training AS A INNER JOIN users AS B ON A.user_id=B.ID INNER JOIN user_courses AS C ON B.ID = C.user_id INNER JOIN user_certificates AS D ON B.ID = D.user_id;
Thanks in Advance.
use left join
select u.* from users u
left join user_training ut on ut.user_id=u.user_id
left join user_courses uc on uc.user_id=u.user_id
left join user_certificates uct on uct.user_id=u.user_id
With this one you are getting all users and their respective trainings:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
Changing *_trainig to *_courses or *_certificates will return all users with respected courses or certificates.
If you need to get data in one query, try this one:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
LEFT JOIN `user_courses` ON `users`.`id` = `user_courses`.`user_id`
LEFT JOIN `user_certificates` ON `users`.`id` = `user_certificates`.`user_id`
If user has no trainings, courses, certificates all remaining fields will be null-ed.

join two tables in CI

i have two tables poolRequests and users
poolRequests containg columns-> fromId(userId),toId(userId),requestType(1/2)
users containg columns-> userId, name
Now I need to join these two tables like
select users.id ,users.name from users
if requestType=1
join poolRequests on users.userId= poolRequests.fromId
if requestType=2
join poolRequests on users.userId= poolRequests.toId
Try this:
select users.id ,users.name
from users
join poolRequests
on (requestType = 1 and users.userId= poolRequests.fromId)
or (requestType = 2 and users.userId= poolRequests.toId)

join other tables

SELECT saleamount/(SELECT SUM(saleamount)
from user_pr where user_id=49) *100 as totalPercentage
from user_pr where user_id=49
i also want to join
select users.firstname from users where type=1
how can i combine both in one query
Take the two table names, put JOIN between then and ON and join condition after the second one.
SELECT saleamount/(SELECT SUM(saleamount)
from user_pr where user_id=49) *100 as totalPercentage,
users.firstname
from user_pr JOIN users ON user_pr.user_id = users.user_id
where user_pr.user_id = 49
and users.type = 1

Select where user roles 1 and 2 SQL query

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

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