MySql Join three tables - mysql

I have join three table into one table but there different column name but same value.
Student Table
-------------
CV_id name
-------------
LC001 ali
LC002 ahmed
LC003 john
LC004 king
Course Table
-------------
Us_id name
-------------
LC001 physic
LC002 maths
LC003 computer
LC004 chemistry
Bridge
-------------
sid CV_cid
-------------
ti LC001
ni LC002
df LC003
ed LC004

Assuming you want to join by the id fields:
select s.name student_name, c.name course_name, b.sid from student s
join course c
on c.us_id = s.cv_id
join bridge b
on b.cv_id = s.cv_id
Here is some information about SQL joins

Use JOIN to achieve your result
SELECT ST.CV_id, ST.name, CO.name, BR.`sid`
FROM Student ST
INNER JOIN Course CO ON CO.Us_id = ST.CV_id
INNER JOIN Bridge BR ON BR.CV_cid = ST.CV_id

Assuming you are joining the table on 1st column as the primary key.
SELECT t1.col, t2.col, t3.col FROM tbl1
join tbl2 ON tbl1.pk = tbl2.pk
join tbl3 ON tbl2.pk = tbl3.pk

YOU CAN USE JOIN
SELECT s.CV_id, s.name, c.name, b.sid
FROM Student s
INNER JOIN Course c ON c.Us_id = s.CV_id
INNER JOIN Bridge b ON b.CV_cid = s.CV_id

Related

Getting all records from INNER JOINS of multiple tables

I have three tables tblCourse , tblDegree , tblStudent. I have created a course and degree relation table as like tblCourseDegreeRelation. This relational table uses foreign keys of course and degree table as like:
The tblCourseDegreeRelation table is like:
The tblCourse table is like:
The tblDegree table is like:
The tblStudent (In this table the degree id is foreign key d_id) table is like:
I need to get all records including the tblStudent all record using this query:
SELECT * from tbldegree d
INNER JOIN tblcoursedegreerelation cdr ON d.d_id = cdr.d_id
INNER JOIN tblcourse c ON cdr.c_id = c.c_id
INNER JOIN tblstudent s ON d.d_id = s.d_id
ORDER BY cdr.cdr_id DESC
But this only returns the one student's record while I've two students in the database see below:
How I can get all students records from the joins query?
In your case you have all inner joins, so it will return results where both/all tables satisfies their criteria (on clause).
Viewing your data your student 1 => Ali has a relation with degree 1 =>BS Information Technology.
Further degree 1 has courses (1 => Programming, 2 => English, 5=> Mathematics , 6 => Electronics)
So for student 1 your inner join clause works because it has data in all joined tables.
Now if we look for your student 3 => Bilal who has a relation with degree 3 => BS Mathematics, But this degree has no assigned courses that is why your student Bilal isn't returned
To get all students no matter their related degree has courses you can turn your inner joins into left join not for all tables but for tblcoursedegreerelation and tblcourse
SELECT *
FROM tblstudent s
INNER JOIN tbldegree d ON d.d_id = s.d_id
LEFT JOIN tblcoursedegreerelation cdr ON d.d_id = cdr.d_id
LEFT JOIN tblcourse c ON cdr.c_id = c.c_id
ORDER BY cdr.cdr_id DESC
Demo
In the result set you can see following columns as null due to no association with courses
cdr_id, c_id, d_id, c_id, c_name, c_credit
Just do a Right join on tblstudent:
SELECT * from tbldegree d
INNER JOIN tblcoursedegreerelation cdr ON d.d_id = cdr.d_id
INNER JOIN tblcourse c ON cdr.c_id = c.c_id
RIGHT JOIN tblstudent s ON d.d_id = s.d_id
ORDER BY cdr.cdr_id DESC
EDIT
This way is better:
SELECT c.d_id,c.d_name,c.d_fee,cdr.cdr_id,cdr_c_id,deg.c_name,deg.d_credit,a.s_id,a.s_name
FROM tblstudent a
left join tblDegree c ON a.d_id = c.d_id
left join tblcoursedegreerelation cdr ON cdr.d_id=c.d_id
left join tblcourse deg on deg.c_id=cdr.c_id
ORDER BY cdr.cdr_id DESC

How to refer without foreign key?

Create two tables:
Course(Course_id(primary key), Course_name)
Student(Roll_no(primary key), Name,Course_id(Foreign key)) and Retrieve the names of all the students who are admitted in the course 'BSC'.
let, the course_id for BSC be 105.
For which the query will be:
SELECT Name FROM Student WHERE Course_id = 105
Can I query for the name of the student without knowing the Course_id (just using the Course_name) ?
You could use an inner join between the tables
SELECT s.Name
FROM Student s
INNER JOIN Course c on c.course_id = s.Course_id
WHERE c.Course_name = 'your_course_name'
or using like
SELECT s.Name
FROM Student s
INNER JOIN Course c on c.course_id = s.Course_id
WHERE c.Course_name like 'your_course_name'
Or use WHERE IN (...)
SELECT
Student.Name
FROM
Student
WHERE
Student.Course_id IN (
SELECT
Course.cource_id
FROM
Course
WHERE
Course.Name = 'BSC'
)
Yes, you can.
SELECT st.Name
FROM Student st
INNER JOIN Course c on c.course_id = st.Course_id
WHERE
c.Course_name = 'Course_Name';

How can I do this relational division query?

I want to do a relational division query. First, here is the structure of each table:
Student
id
name
Course
id
name
Student_passed_course (junction table that stores who passed which course)
id_student
id_course
Basically, what I want is to get the names of the students that have passed all the courses that exist in table Course using JOIN (or LEFT JOIN, etc). I already implemented a solution using NOT EXISTS.
Also this is the equation I made.
A solution based on JOIN could be as follow:
SELECT s.name AS student_name, c.name as course_name FROM (
SELECT s_id, c_id, SUM(c_exists) AS c_exists, SUM(c_taken) as c_taken, SUM(c_exists)-SUM(c_taken) as not_taken FROM (
SELECT s.id as s_id, c.id as c_id, 1 as c_taken, 0 as c_exists FROM student s JOIN student_passed_course spc ON spc.id_student = s.id JOIN course c ON c.id = spc.id_course
UNION ALL
SELECT s.id as s_id, c.id as c_id, 0 as c_taken, 1 as c_exists FROM student s JOIN course c
) X group BY s_id, c_id) Y
JOIN student s ON s.id = s_id JOIN course c ON c_id = c.id
WHERE not_taken = 1;
I don't know if this would be more or less efficient than your solution.

mysql join two tables with registry table using join query

i want to join 2 mysql tables .but join information is in a separate table .let's say i have 3 table named student ,course and reg contain id of student and course he does.
student table
s_id | name
1 | miki
2 | foly
3 | oski
course table
c_id | name
101 | c++
102 | java
103 | ruby
reg table
s_id | c_id
1 | 101
1 | 102
2 | 101
now i want to get all the course someone do.i wrote sql query for that without using join query .but i want to do same thing using join query .this is my query
SELECT c.name FROM student as s,course as c,reg as r where r.s_id=s.s_id and r.c_id=c.c_id and s.name='miki';
Statement
SELECT c.name
FROM student as s,couse as c,reg as r
where r.s_id=s.s_id and r.c_id=c.c_id and s.name='miki'
is join too, , between table names is short cut for cross join, so you already using joins (actually you have some conditions in where, so RDBMS will optimize it to inner join)
but, of course you can rewrite it to different syntax:
SELECT c.name
FROM couse as c
inner join
reg as r
on (c.c_id = r.c_id and r.s_id=(select s_id from student where name='miki'));
another syntax:
SELECT c.name
FROM couse as c
inner join
reg as r
on (c.c_id = r.c_id)
inner join
student as s
on (r.s_id=s.s_id and s.name='miki');
and another one:
SELECT c.name
FROM couse as c
inner join
reg as r
on (c.c_id = r.c_id)
inner join
student as s
on (r.s_id=s.s_id)
where s.name='miki';
depending on bunch of conditions performance of these 4 queries can be different, but results will be the same
Just join all 3 tables to get the result
select c.name
from course c
join reg r on r.c_id = c.c_id
join student s on s.s_id = r.s_id
where s.name = 'miki'
SELECT s.name,
c.name
FROM student s
JOIN reg r
ON r.s_id = s.s_id
JOIN course c
ON c.c_id = r.c_id
WHERE s.name = "miki"
SELECT c.name, s.name as stud FROM course c
JOIN reg r on c.c_id = r.c_id
JOIN student s on r.s_id = s.s_id
WHERE s.name = 'miki'

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;