Issues with inner join in mysql - mysql

I want to join output of one query with the second table in mysql:
(select
A.name, A.address, sum(C.penalty_points) as points
from
restaurant as A
inner join
inspection as B ON A.restaurant_id = B.restaurant_id
inner join
violation as C ON C.violation_id = B.violation_id
group by A.name )
Output:
name address points
Kitchen 305 660 Washington Ave 2
PL8 Kitchen Southeast 17th Street in Fort Lauderdale 11
Prime One Twelve 112 Ocean Drive 5
Seasons 52 Palm Beach Gardens 3
Six Tables 32 East Atlantic 8
Table 26 Park Racks Downtown Eatery 2
The result of the second table:
select * from health_points
Output:
points health_grade
0 A
1 A
2 A
3 A
4 A
5 B
6 B
7 B
8 B
9 B
10 B
11 C
12 C
13 C
14 C
15 C
17 FAIL
18 FAIL
19 FAIL
Is there a way I can combine the first query with the second table and extract the health grade ? I was trying something like:
(select
A.name, A.address, sum(C.penalty_points) as points
from
restaurant as A
inner join
inspection as B ON A.restaurant_id = B.restaurant_id
inner join
violation as C ON C.violation_id = B.violation_id
group by A.name ) as D inner join health_points as E on D.points = E.points
But it is showing error in mysql? Any pointers where I am going wrong here?

You are missing the outer SELECT clause:
SELECT D.*, E.health_grade
FROM (
SELECT A.name, A.address, sum(C.penalty_points) as points
FROM restaurant A
JOIN inspection B ON (A.restaurant_id = B.restaurant_id)
JOIN violation C ON (C.violation_id = B.violation_id)
GROUP BY A.name
) D
JOIN health_points E ON (D.points = E.points)

You can do this:
SELECT
e.health_grade,
d.points
FROM
(
select
A.name, A.address, sum(C.penalty_points) as points
from restaurant as A
inner join inspection as B ON A.restaurant_id = B.restaurant_id
inner join violation as C ON C.violation_id = B.violation_id
group by A.name, A.address
) as D
inner join health_points as E on D.points = E.points

Related

Mysql query count total from other table

Table jenis_usaha
ID JENIS_USAHA
1 Laundry
2 Restauran
Table waralaba
ID ID_JENIS_USAHA
1 1
2 2
Table perusahaan
ID NAME ID_WARALABA
1 A 1
2 B 2
3 C 1
4 D 1
Table outlet
ID ID_PERUSAHAAN
1 1
2 1
3 2
4 2
5 1
6 1
7 1
8 1
9 1
The result that I want is like this
ID JENIS_USAHA TOTAL_PERUSAHAAN TOTAL_OUTLET
1 Laundry 3 7
2 Restauran 1 2
I have this mysql query
SELECT ju.ID,
ju.JENIS_USAHA,
COUNT(p.ID) AS TOTAL_PEMBERI,
(SELECT count(o.ID)
FROM outlet o
WHERE p.ID = o.ID_PERUSAHAAN
AND w.ID = p.ID_WARALABA
AND ju.ID = w.ID_JENIS_USAHA
) AS TOTAL_OUTLET
FROM jenis_usaha ju
LEFT JOIN waralaba w ON w.ID_JENIS_USAHA = ju.ID
LEFT JOIN perusahaan p ON p.ID_WARALABA = w.ID
GROUP BY ju.ID
I've got no error BUT a wrong result for TOTAL_OUTLET. Could you please show me the right query for this? thanks
you did the wrong join that's why you got wrong output
below query should work as your way
select j.id ,j.name,TOTAL_PERUSAHAAN,outlet as TOTAL_OUTLET
from jenis_usaha j inner join
(select p.id,count(o.ID_PERUSAHAAN) as outlet from outlet o
inner join perusahaan p
on o.ID_PERUSAHAAN=p.id
group by p.id
) t1
on t1.id=j.id
inner join
(
select w.id, count(p.ID_WARALABA) as TOTAL_PERUSAHAAN
from waralaba w inner join perusahaan p
on w.id=p.ID_WARALABA
group by w.id
) t2
on t2.id=j.id
id name TOTAL_PERUSAHAAN TOTAL_OUTLET
1 Laundry 3 7
2 Restauran 1 2
here is fiddle link where you can find details
http://sqlfiddle.com/#!9/400c971/6
I was taking very long to understand your query, so I decided to do my own.
This one bring the results you wanted anyway. Please check if this works for your case. =)
Btw, st stands for subtotal
SELECT
st.id,
st.name,
count(ps_id) AS ps_amount,
sum(ol_amount) AS ol_amount
FROM
(SELECT
ju.id,
ju.name,
ps.id AS ps_id,
count(ol.id) as ol_amount
FROM
jenis_usaha ju
INNER JOIN
waralaba wl
ON wl.id_jenis_usaha = ju.id
LEFT JOIN
perusahaan ps
ON ps.id_waralaba = wl.id
LEFT JOIN
outlet AS ol
ON ol.id_perusahaan = ps.id
GROUP BY
ju.id,
ju.name,
ps.id) st
GROUP BY
st.id,
st.name

SQL How to join two tables and extract result

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

How to return null if almost one inner join condition doesn't have value?

I have a table called league_ranking which contains the ranking of a specific round for all the teams available within a competition.
Now happen that some rounds doesn't have any value in the league_ranking table, so in this case, I want to prevent that in the list of the competitions appear the competition that have almost a round without value in league_ranking.
This is my query:
SELECT c.name AS competition_name,
c.id AS competition_id
FROM competition c
INNER JOIN competition_seasons s ON s.competition_id = c.id
INNER JOIN competition_rounds r ON r.season_id = s.id
INNER JOIN league_ranking l ON l.round_id = r.id
WHERE c.country_id = :country_id
GROUP BY c.id
ORDER BY c.name ASC
Data example and table structure
league_ranking
|position | team_id | round_id |
1 120 5
2 124 5
competition_rounds
| id | season_id | name
5 577 First Round
6 578 Preliminary Round
competition_seasons
|id | competition_id
577 28
578 28
competition
|id | name
28 Premier
as you can see the round 6 don't have values inside league_ranking, the problem is that my query even returns the competition Premier, how can I instead prevent to return this competition that have almost a round without value?
Thanks.
You can try to use LEFT JOIN instead of INNER JOIN, You need to Outer JOIN base on competition_rounds table.
SELECT c.name AS competition_name,
c.id AS competition_id
FROM competition_rounds r
LEFT JOIN competition_seasons s ON r.season_id = s.id
LEFT JOIN competition c ON s.competition_id = c.id
LEFT JOIN league_ranking l ON l.round_id = r.id
WHERE c.country_id = :country_id
GROUP BY c.id
ORDER BY c.name ASC
sqlfiddle

what is the sql query?

staff table:
id name
1 tony
2 sony
3 bony
4 rony
etc.
xyz table:
current staff, next staff, hod staff, vp staff.
1 2 3 4
2 3 1 4
4 3 2 1
etc.
when is run
`select * from xyz,`
I need a query which instead of giving me id it will actually give me names of that staff from staff table.
You need to join staff table more than once
SELECT b.name AS current_staff,
d.name AS next_staff,
c.name AS hod_staff,
e.name AS vp_staff
FROM xyz a
INNER JOIN staff b
ON a.current_staff = b.id
INNER JOIN staff c
ON a.hod_staff = c.id
INNER JOIN staff d
ON a.next_staff = d.id
INNER JOIN staff e
ON a.vp_staff = e.id
If I understand you right, I guess you just could use a multiple join here:
select a.name, b.name, c.name, d.name from xyz
inner join staff as a on a.id = xyz.currentstaff
inner join staff as b on b.id = xyz.nextstaff
inner join staff as c on c.id = xyz.hodstaff
inner join staff as d on d.id = xyz.vpstaff

Intersection in mysql for multiple values

The intersect keyword is not available in mysql. I want to know how to implement the following in mysql db. My tables are:
customer(cid,city,name,state)
orders(cid,oid,date)
product(pid,price,productname)
lineitem(lid,pid,oid,totalquantity,totalprice)
I want the products bought by all the customers of a particular city 'X'. i.e. every customer in city 'x' should have bought the product. I managed to select the oid's and the pid's of customers living in that particular city. Now I should select the pid's which is present in all the oid's.
Example.
Oid Pid
2400 1
2400 2
2401 3
2401 1
2402 1
2403 1
2403 3
The answer from the above input should be 1 because it is present in all oid's. The query which I used to get the oid's and pid's:
select t.oid,l.pid
from lineitem l
join (select o.oid,c1.cid
from orders o
join (select c.cid
from customer c
where c.city='X') c1
where o.cid=c1.cid) t on l.oid=t.oid
Now I need to intersect all the oid's and get the result.The query should not be dependent on data.
Try:
select pid, count(*)
from (select t.oid, l.pid
from lineitem l
join (select o.oid, c1.cid
from orders o
join (select c.cid from customer c where c.city = 'X') c1
where o.cid = c1.cid) t
on l.oid = t.oid) x
group by pid
having count(*) = (select count(*)
from (select distinct oid
from lineitem l
join (select o.oid, c1.cid
from orders o
join (select c.cid
from customer c
where c.city = 'X') c1
where o.cid = c1.cid) t
on l.oid = t.oid) y) z
I think you can achieve what you want by using IN