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'
Related
Here is my Airport Table:
id code airport
1 MAA Chennai Airport
2 DEL Delhi Airport
Here is my Equipment Table:
id type Desc
1 Q400 Q Series
2 B737 B Series
And Here is my Schedule Table:
id station equipment
1 1 2
2 2 1
Here is my expected result:
id station equipment
1 MAA B737
2 DEL Q400
How can I do this ?
This is what I have tried so far:
select schedule.id, schedule.station, flight_schedules.equipment
inner join airport where schedule.station = airport.code
How can I get the expected result.
You can use INNER JOIN or LEFT JOIN according to your requirements as indicated on the diagram below. On the other hand, you can follow this approach in order to join multiple tables:
SELECT * FROM
A INNER JOIN
B INNER JOIN
C INNER JOIN
D INNER JOIN
E INNER JOIN F
ON F.eid = E.id
ON E.did = D.id
ON D.cid = C.id
ON C.bid = B.id
ON B.aid = A.id
Here is the diagram for understanding JOIN types in MSSQL below:
You can do:
SELECT s.id, a.code AS station, e.type AS equipment FROM Schedule s
LEFT JOIN Airport a ON a.id = s.station
LEFT JOIN Equipment e ON e.id = s.equipment
i dont know which language do you use but i brought this example which uses sql language and may be it works for some other languages or all of them:-
SELECT Schedule.id, Airport.code, Equipment.type
FROM ((Schedule
INNER JOIN Airport ON Schedule.station = Airport.id)
INNER JOIN Shippers ON Schedule.equipment = Equipment.id);
*Hallo there,
here is the right answer, what you want to have.
But your expacted result is wrong, because you put equipmnet id 2 in table schedule. That why the answer look different by me *
The right code
SELECT Sch.[equipment]
,Air.code
,Equ.type
FROM [Test].[dbo].[Scheule] as Sch
INNER JOIN [Test].[dbo].[Airport] as Air
ON Air.id = Sch.station
INNER JOIN [Test].[dbo].[Equipment] as Equ
ON Equ.id = Sch.id
enter image description here
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
Say I have these data from two table:
Student Table columns:
id | name
Course Table columns:
id | code | name
and I want to use the Student.id AS Student and Course.id AS Course
to get the following:
Student | Course
-----------------
1 | C
1 | B
1 | A
2 | F
2 | B
2 | A
3 | C
3 | B
3 | F
How would I query it so it will return only the Students with a Course C and their other Courses like below:
Student | Course
-----------------
1 | C
1 | B
1 | A
3 | C
3 | B
3 | F
?
I have tried :
SELECT Student.id, Course.code FROM Course
INNER JOIN Student ON Course.student = Student.id
WHERE Course.code = 'C'
but I got only
Student | Course
-----------------
1 | C
3 | C
SELECT s.id, c.code
FROM Course c
INNER JOIN Student s
ON c.student = s.id
WHERE EXISTS
(
SELECT 1
FROM Course c1
WHERE c.student = c1.student
AND c1.Course = 'C'
)
The most efficient approach to this problem is usually an inline view and a JOIN operation, although there are several ways to get an equivalent result.
SELECT Student.id
, Course.code
FROM ( SELECT c.Student
FROM Course c
WHERE c.code = 'C'
GROUP BY c.Student
) o
JOIN Course
ON Course.Student = o.Student
JOIN Student
ON Student.id = Course.Student
Here, we're using an inline view (aliased as o) to get a list of Student taking course code = 'C'.
(NOTE: the query in my answer is based on your original query. If there's a foreign key definition between Course and Student, and we only need to return the Student.id, we could improve performance by omitting the join to Student, and return Course.Student AS id in place of Student.id in the SELECT list.)
Here the first JOIN selects only those students which have course C, and second JOIN gives you all the courses for each of those students.
SELECT st.id, c2.code FROM
Student st
JOIN Course c ON c.student = st.id AND c.code = "C"
JOIN Course c2 ON c2.student = st.id
You actually don't even need two tables here, because both student and course is available in the Course table, just JOIN it on itself:
SELECT c2.student, c2.code FROM
Course c JOIN Course c2 ON c.student = c2.student
WHERE c.course = "C"
Here the WHERE clause leaves student id's which have course C and then you JOIN those to find all their courses.
Hi everyone I would like to count some entries, but I don't know if is possible to do it with joins. The situation is
I've got a tables
student_profile
Id | Name
---------
1 | Name1
2 | Name2
3 | Name3
student_application where profile_id is related to student_profile.id
Id | profile_id | Data
----------------------
1 2 data1
2 2 data2
3 2 data3
And table student_holiday
Id | app_id | date
-----------------------
1 2 2014-01-01
2 3 2014-02-02
So I'm getting all my student_application's with
Select sa.id, s.name From student_application sa
INNER JOIN student_profile s ON s.id = sa.profile_id
I would like to count how many holidays has a student, but I don't have profile_id in student_holiday table. There I've got app_id, so I can't do Left join student_holiday sh ON sh.app_id = sa.id, this wouldn't give the right number.
here sqlfiddle
Thanks in advance for any help.
SELECT student_profile.ID, student_profile.Name, student_holiday.ID
FROM (student_profile INNER JOIN student_holiday ON student_profile.ID = student_holiday.ID) INNER JOIN student_application ON student_holiday.ID = student_application.ID;
Here you go!!! Match ID in all the table as instructed above which is the key to get holidays.
OR match with profile_id
**SELECT student_profile.ID, student_profile.Name, student_holiday.ID
FROM (student_profile INNER JOIN student_holiday ON student_profile.ID = student_holiday.ID) INNER JOIN student_application ON Trim(student_holiday.ID) =
student_application.profile_id;**
Here is your data with the 3rd table joined:
Select s.name, sa.some_data as application_data,h.some_data as holiday_data
From student_application sa
INNER JOIN student_profile s ON s.id = sa.profile_id
INNER JOIN student_holiday h ON h.app_id = sa.id;
Note this will only return records that have an application with a holiday associated.
Then to count them, we just:
Select s.name, sa.some_data as application_data,count(h.some_data) as holiday_data
From student_application sa
INNER JOIN student_profile s ON s.id = sa.profile_id
INNER JOIN student_holiday h ON h.app_id = sa.id
GROUP BY s.name
Note this answer is based on the fieldnames you used in your sqlfiddle (Data = some_data and date = some_data)
In MySql database have AM_COURSE table in that
cId CourseName course_desc
101 java sometext...
102 mysql sometext....
:
AM_USER in that
uid name
1001 Ram
1002 pavan
AM_INTER in that
id uid cid
1 1001 101
2 1001 102
3 1002 101
AM_TIMETABLE table in that
UserId Date_Time
101 2012-08-08 04:00:00
102 2012-08-15 10:00:00
103 2012-08-18 09:00:00
104 2012-08-24 05:00:00
My Question is, i'm passing start-date and end-date and CourseName then i want to get details like userId,name,coursename,course_desc in that way i wrote query
SELECT a.cid,a.course_desc,c.name
FROM
AM_COURSE a,AM_USER c INNER JOIN AM_TIMETABLE b ON a.S_ID = b.COURSE_ID WHERE
a.Name = 'java'
AND
b.Date_Time BETWEEN '2012-08-07 00:00:00' AND '2012-08-20 00:00:00'
but i'm getting error please help
The INNER JOIN keyword returns rows when there is at least one match in both tables.
MySQL INNER JOIN clause
The MySQL INNER JOIN clause matches rows in one table with rows in other tables and allows you to query rows that that contain columns from both tables.
The MySQL INNER JOIN clause an optional part of the SELECT statement. It appears immediately after the FROM clause.
Before using MySQL INNER JOIN clause, you have to specify the following criteria:
First, you have to specify the main table that appears in the FROM clause.
Second, you need to specify the tables that you want to join with the main table, which appear in the INNER JOIN clause. Theoretically, you can join a table with many tables. However, for a better query performance, you should limit the number of tables to join.
Third, you need to specify the join condition or join predicate. The join condition appears after the keyword ON of the INNER JOIN clause. The join condition is the rule for matching rows between the main table and the other tables.
Example
SELECT c.cid, c.course_desc, user.name FROM AM_COURSE c
INNER JOIN AM_INTER inter on inter.cid = c.cid
INNER JOIN AM_USER user on user.uid = inter.uid
INNER JOIN AM_TIMETABLE tt ON inter.cid = tt.UserId
WHERE c.Name = 'coursename' AND tt.Date_Time BETWEEN '2011-08-12' AND '2012-08-12'
try
SELECT course.cid, course.course_desc, user.name
FROM AM_COURSE course
INNER JOIN AM_INTER inter on inter.cid = course.cid
INNER JOIN AM_USER user on user.uid = inter.uid
INNER JOIN AM_TIMETABLE tt ON inter.cid = tt.UserId
WHERE course.Name = 'java'
AND tt.Date_Time BETWEEN '2012-08-07' AND '2012-08-20'