SQL How to join two tables and extract result - mysql

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

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

Trying to get a count in 3 tables on MySql

So I have 3 tables in a database
One is hotel which has hotel_id and status
one is partner which has partner_id and partner_name
and one is partner_hotel which has hotel_id and partner_id
What I am trying to get is the count for each partner that has a hotel with the status = 1
The closest I have gotten is
select p.partner_name,count(hotel_id)
from partner_hotel ph
join partner p on p.partner_id = ph.partner_id
group by ph.partner_id;
The problem is that does not limit to ones with a status of 1 and nothing I seem to be doing seems to work.
you should join hotel too if th hotel contain status and group by p.partner_id
select p.partner_name,count(*)
from partner_hotel ph
inner join partner p on p.partner_id = ph.partner_id
inner join hotel h on ph.hotel_id = h.hotel_id
where h.status = 1
group by p.partner_id;
or ig hotel use id
select p.partner_name,count(*)
from partner_hotel ph
inner join partner p on p.partner_id = ph.partner_id
inner join hotel h on ph.hotel_id = h.id
where h.status = 1
group by p.partner_id;
I guess you're trying to find the number of partners with one or more hotels having a status = 1. If my guess is correct, this gets you the one number.
SELECT COUNT(DISTINCT p.partner_id) partner_count
FROM partner p
JOIN partner_hotel ph ON p.partner_id = ph.partner_id
JOIN hotel h ON ph.hotel_id = h.hotel_id AND h.status = 1
If your requirement is to show one result row for each partner showing the number of hotels with status = 1, that will go like this.
SELECT COUNT(*) hotel_count,
p.partner_id
FROM partner p
JOIN partner_hotel ph ON p.partner_id = ph.partner_id
JOIN hotel h ON ph.hotel_id = h.hotel_id AND h.status = 1
GROUP BY p.partner_id
Pro tip: When preparing to write a query, it's helpful to imagine the result set you want. Ask yourself these questions:
How many rows must the result set have?
It should have one row per ____ (row per what? Hotel, Partner, Guest, Hotel with status = 1?).
Each row should show ____ values?
If you do this work of imagination, writing the query will be much easier.

MySQL: how to combine three tables

I have a table with persons (personid,name), another table with camps (campid,title) and a third table with camp participations (id,personid,campid).
Now, for a given camp, I need a list of all other camp participations by all persons participating in the current camp.
But I have no idea how to join these tables. I have looked at a lot of other examples, but I can't really seem to get any inspiration from those...
This should work:
SELECT *
FROM PERSONS AS D INNER JOIN CAMP_PARTICIPATIONS AS E ON D.PERSONID = E.PERSONID
INNER JOIN CAMPS AS F ON F.CAMPID = E.CAMPID
WHERE F.CAMPID <> [your_camp] AND A.PERSONID IN (
SELECT A.PERSONID
FROM PERSONS AS A INNER JOIN CAMP_PARTICIPATIONS AS B ON A.PERSONID = B.PERSONID
INNER JOIN CAMPS AS C ON C.CAMPID = B.CAMPID
WHERE C.CAMPID = [your_camp] )
select persons.*,participations.*,camps.* from persons left join participations
on participations.personid=persons.personid
left join camps on camps.campid=participations.campid
where camp.campid=1;
now u can change the campid in where clause and place column name which u want in select clause

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'

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