Join two table where one table has relation with many table - mysql

I have 5 table, and the structure is below --
user - id
- name
- dob
- phone
desi - id
- desgname
- dept_id
dept - id
- deptname
- org_id
org - id
- orgname
junction_table - id
- userid
- desg_id
- dept_id
- org_id
How could I make a joint table?
update
After trying sometime, I got a solution and i posted it as answer

Try this
$this->db->select('your-selection');
$this->db->from('user');
$this->db->join('designation','user.designation_id=designation.designation_id');
$this->db->join('department','user.department_id=department.department_id');
$this->db->join('org','user.org_id=org.org_id');
$this->db->join('designation','user.designation_id=designation.designation_id');
$this->db->where('your-condition`);
$result_set = $this->db->get();
return $result_set->result();
your-selection can be your fields that you want to select.
your-condition can be your conditions in an array.

If I understood well I think you need something like this, join every table to get the info you need.
SELECT
U.name
,D.desgname
,DPT.deptname
,O.orgname
FROM junction_table JT
INNER JOIN [user] U
ON JT.userid = U.id
INNER JOIN [desi] D
ON JT.desg_id = D.id
INNER JOIN [dept] DPT
ON JT.dept_id = DPT.id
INNER JOIN org O
ON JT.org_id = O.id

The Solution is to left join the junction table to user table and then again left join to junction table to other table
SELECT
U.name,
O.orgname,
DPT.dpt,
D.desgname
FROM user U
LEFT JOIN junction_table JT
ON U.id = JT.user_id
LEFT JOIN organization O
ON JT.org_id = O.id
LEFT JOIN tbl_suborg DPT
ON JT.dpt_id = DPT.id
LEFT JOIN designation D
ON JT.desg_id = D.id
Thanks brother for trying to help me.

Related

SQL - inner join on different criteria

Just getting confused on basic stuff -
could someone explain me this -
select s.name from students s
inner join friends f on f.id = s.id
inner join packages p on p.id = s.id
where p.salary < (select pp.salary from packages pp where pp.id = f.friend_id)
order by (select pp.salary from packages pp where pp.id = f.friend_id) ASC;
the salary comparison part - i.e select pp.salary from packages pp where pp.id = f.friend_id should not yield the same salary result? - so how can we compare.
for references, use the below sample tables
table 1- students
columns - id, name
table 2 - friends (here each id is linked with one friend_id (his best friend))
columns - id , friend_id
table3 - packages
columns - id , salary
Trying to find out the name of the friend whose best friend's salary is more than his salary.
I am confused at understanding this solution.
That where subquery part is wrong cause the subquery will return multiple record and which can't be used with < operator since it's accepts scalar value. Rather change that to a JOIN as well like
JOIN packages pp ON pp.id = f.friend_id
AND p.salary < pp.salary
Change your query to be
select s.name from students s
inner join friends f on f.id = s.id
inner join packages p on p.id = s.id
JOIN packages pp ON pp.id = f.friend_id
AND p.salary < pp.salary
order by pp.salary;

left join table name dynmicly form the main query

I have a bills table with column customer_type and customer_id fields.
This customer_type tells if the customer is in the customers table or in the users table or in the suppliers table.
I need to create a query with left join according to customer_type.
select c.* from bills b
left join ***b.customer_type*** c on c.id = b.customer_id
You could join all three with necessary condition:
select c.*, u.*, s.* from bills b
left join customers c on c.id = b.customer_id and b.customer_type = 'customers'
left join users u on u.id = b.customer_id and b.customer_type = 'users'
left join suppliers s on s.id = b.customer_id and b.customer_type = 'suppliers'
Then you can take the data that is relevant from the result.
However if there are similar columns in these 3 tables you might want to restructure the database to only store one type of information in one place.

Multiple foreign keys on same column

I have following tables:
Table: user
Columns
- id
- username
- full_name
Table: pet
Columns
- id
- pet_name
Table: results
Columns
- id
- user_id_1
- user_id_2
- user_id_3
- pet_id
- date
- some_text
Can I make an SQL query witch will give me one row with: id, full_name, full_name, full_name, pet_name, date, some_text where the results.id is 3?
select u1.full_name
, u2.full_name
, u3.full_name
, p.pet_name
, r.date
, r.some_text
from Results r
join User u1
on u1.id = r.user_id_1
join User u2
on u2.id = r.user_id_2
join User u3
on u3.id = r.user_id_3
join pet p
on p.id = r.pet_id
where r.id = 3
Try Following query :
SELECT A.id, B.full_name, C.full_name, D.full_name, E.pet_name, A.date, A.some_text
FROM RESULTS AS A
LEFT OUTER JOIN USER AS B ON A.USER_ID1 = B.ID
LEFT OUTER JOIN USER AS C ON A.USER_ID2 = C.ID
LEFT OUTER JOIN USER AS D ON A.USER_ID3 = D.ID
LEFT OUTER JOIN PET AS E ON A.PET_ID = E.ID
WHERE A.id = 3

MySQL `INNER JOIN` multiples of the same table

Is it possible to INNER JOIN a MySQL query to achieve this result?
I have a table with Strategies and a table with Members. The Strategy table holds the ID of the author that corresponds to their ID in the Member table and the ID of an author that updated the existing author's work. Is it possible to grab a reference to both of these people at the same time? Something like the following, which returns no errors, but also no results...
SELECT * FROM Strategies
INNER JOIN Members AS a
INNER JOIN Members AS b
WHERE Strategies.ID='2'
AND Strategies.AuthorID = a.ID
AND Strategies.UpdateAuthorID = b.ID
Use a LEFT JOIN:
SELECT
s.*,
a.Name AS MemberName,
b.Name AS UpdatedMemberName
FROM Strategies AS s
LEFT JOIN Members AS a ON s.AuthorID = a.ID AND s.ID = 2
LEFT JOIN Members AS b ON s.UpdateAuthorID = b.ID AND s.ID = 2 ;
If you want them in one column use COALESCE:
SELECT
s.*,
COALESCE(a.Name, b.Name) AS MemberName
FROM Strategies AS s
LEFT JOIN Members AS a ON s.AuthorID = a.ID AND s.ID = 2
LEFT JOIN Members AS b ON s.UpdateAuthorID = b.ID AND s.ID = 2
SELECT toD.dom_url AS ToURL,
fromD.dom_url AS FromUrl,
rvw.*
FROM reviews AS rvw
LEFT JOIN domain AS toD
ON toD.Dom_ID = rvw.rev_dom_for
LEFT JOIN domain AS fromD
ON fromD.Dom_ID = rvw.rev_dom_from
if domain is table name

How to get data from 4 tables in 1 sql query?

I have the following database schema:
table courses:
id
tutor_id
title
table course_categories:
id
category_id
course_id
table categories:
id
name
table tutors:
id
name
table subscribers:
id
course_id
user_id
I need to make 1 sql to get a course with all it's categories, and the tutor for that course and the number of subscribers for that course. Can this be done in 1 query? Should this be done using stored procedures?
With this query you get what you want:
select co.title as course,
ca.name as category,
t.name as tutor,
count(s.*) as total_subscribers
from courses co
inner join course_categories cc on c.id = cc.course_id
inner join categories ca on cc.category_id = ca.id
inner join tutors t on co.tutor_id = t.tutor_id
left join subscribers s on co.id = s.course_id
where co.title = 'Cat1'
group by co.title, ca.name, t.name
I used left join on subscribers because there might be no one for a given course. I'm assuming that all the other tables have data on it for every course, categorie and tutor. If not, you can user left join as well but then you'll have data with null.
It can be done. You need to look up select and the use of join. See select and join to help complete the assignment
select cou.title, cat.name, tu.name, count(sub.user_id) from courses cou, course_categories cca, categories cat, tutors tu, subscribers sub where cou.id = cca.id and cat.id = tu.id and tu.id = sub.id group by cou.title, tu.name;