join two tables in CI - php

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)

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.

SQL - join selected data or table for conditions

I have SQL tables for users and relationships, like
users{ID, name, email}
relationships{ID, uid, relid, type}
I want to get all "friends" ID, name and email (by current user ID) by SQL, using JOIN or whatever.
If using join is it better to JOIN the table of users and select relationships or select relationships and join users?
something like this (I invented now, i use a bit diferrent tables and code, sorry for errors)
1:
SELECT DISTINCT u.ID, u.name, u.email FROM users u JOIN relationships r ON (r.uid = 1 OR r.relid = 1) AND r.type = 1 WHERE u.ID = r.uid OR u.ID = r.relid
2:
SELECT DISTINCT u.ID, u.name, u.email FROM relationships r JOIN users u ON r.uid = u.ID OR r.relid = u.ID = 1 WHERE r.uid = u.ID OR r.related = u.ID
Edit:
I use DISTINCT becouse when I select users whoose IDs equals to r.uid or r.related, I also get the current user (uid + related) for every relation. DISTINCT should unique the users? I'm beginner and found this on stackoverflow.
So, is it better to select from users and join relations or select relations and join users?
BTW: I quite like the answer with UNION
You can union the users where user is in the uid and in the relid.
SELECT users.*
FROM users
INNER JOIN relationships AS rel
ON users.ID = rel.uid
WHERE users.ID = 500
UNION
SELECT users.*
FROM users
INNER JOIN relationships AS rel
ON users.ID = rel.relid
WHERE users.ID = 500
Not sure why you are using DISTINCT but it looks like you just want to select all the rows from the relationships table (the friends), and then join their details from the users table.
SELECT u.id, u.name, u.email
FROM relationships r
LEFT JOIN users u ON u.id = r.uid
WHERE r.uid = 12345

Multiple table inner join need first table id

Working that query. Than i need projects table's id. echo $row['id'] is printing users->id. But i need print projects->id.
SELECT * FROM projects INNER JOIN users ON projects.manager = users.id
You can create alias like below:
SELECT *, projects.id as prodict_id, users.id as user_id FROM projects INNER JOIN users ON projects.manager = users.id
then you can use it
$row['prodict_id']

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

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

Categories