Display all results of SQL query (including where count 0) - mysql

I have the following Query:
SELECT airportname, COUNT(DISTINCT foundBag.id) countFound, COUNT(DISTINCT lostBag.id) countLost
FROM airports
INNER JOIN foundBag ON airport_id = foundBag.airportDest
INNER JOIN lostBag ON airport_id = lostBag.airportDest
GROUP BY airport.airportname");
What I have now: A table that has 3 columns: Airport name with the number of bags found and number of bags lost.
It only displays an airport (row) when both of the columns are filled.
I want 2 things:
To display all the airports even when there are no lost / found bags.
To display the airports where the are lost / found bags (so when 1 or both the columns are filled)
I tried this with When etc. but it keeps giving me errors. I also tried the EXISTS but I'm new to SQL so I do not know how it works...
Does someone have a solution?
Kind regards,
LTKort

This should work for both conditions:
SELECT airportname, COUNT(DISTINCT foundBag.id) countFound, COUNT(DISTINCT lostBag.id) countLost
FROM airports
LEFT JOIN foundBag ON airport_id = foundBag.airportDest
LEFT JOIN lostBag ON airport_id = lostBag.airportDest
WHERE foundBag.airportDest is not null
or lostBag.airportDest is not null
GROUP BY airport.airportname

Related

Mysql Query with two seperate join

Does anyone know the solution to this problem ?
There are 3 Tables: orders, order_groups and stores.
I want to list the orders, with the names of the stores where the order was placed, and where the order is going to be delivered.
I keep the from_store_id, and to_store_id in the order_groups table
Listing these orders would be simple, i just left join the order_groups to orders, and select the name, from_shop_id and to_shop_id, but the problem is i want the name of the stores not the id, and the store names are placed in a different table (stores)
Here is what im talking about:
Table orders
id group_id name madeup_id
1 11 johnny cash 1
2 12 billy bob 1
LEFT JOIN order_groups on order_groups.id = orders.group_id
Table order_groups
id from_store_id to_store_id
11 55 56
12 56 55
Table stores
id store_name
55 thisstore
56 thatstore
The result im looking for is:
name from_store to_store
1.johhny cash thisstore, thatstore
2.billy bob thatstore, thisstore
The statement i have yet:
SELECT
orders.name, something as from_store, something as to_store
FROM orders
LEFT JOIN order_groups on order_groups.id = orders.group_id
somehow join stores on the order_groups.from_store_id = stores.id
WHERE orders.madeup_id = 1
Any idea how to select and join the store names to the query ?
One more question. I actually want to list two kind of orders in one query from different tables too, im on the right track with this structure ?
SELECT a,b FROM a LEFT JOIN b ON b.something=a.something WHERE something
UNION ALL
SELECT a,b FROM c LEFT JOIN c ON c.something=a.something WHERE something
You only need to join 2 times the same table!
SELECT
orders.name, fromStore.store_name as from_store, toStore.store_name as to_store
FROM orders
LEFT JOIN order_groups on order_groups.id = orders.group_id
left join stores fromStore on the order_groups.from_store_id = fromStore.id
left join stores toStore on the order_groups.to_store_id = toStore.id
WHERE orders.madeup_id = 1

Optimisation of subqueries

I have a relation between users and groups. Users can be in a group or not.
EDIT : Added some stuff to the model to make it more convenient.
Let's say I have a rule to add users in a group considering it has a specific town, and a custom metadata like age 18).
Curently, I do that to know which users I have to add in the group of the people living in Paris who are 18:
SELECT user.id AS 'id'
FROM user
LEFT JOIN
(
SELECT user_id
FROM user_has_role_group
WHERE role_group_id = 1 -- Group for Paris
)
AS T1
ON user.id = T1.user_id
WHERE
(
user.town = 'Paris' AND JSON_EXTRACT('custom_metadata', '$.age') = 18
)
AND T1.user_id IS NULL
It works & gives me the IDs of the users to insert in group.
But when I have 50 groups to proceed, like for 50 town or various ages, it forces me to do 50 requests, it's very slow and not efficient for my Database.
How could I generate a result for each group ?
Something like :
role_group_id user_to_add
1 1
1 2
2 1
2 3
The only way I know to do that for now is to do an UNION on several sub queries like the one above, but of course it's very slow.
Note that the custom_metadata field is a user defined field. I can't create specific columns or tables.
Thanks a lot for your help.
if I good understood you:
select user.id, grp.id
from user, role_group grp
where (user.id, grp.id) not in (select user_id, role_group_id from user_has_role_group) and user.town in ('Paris', 'Warsav')
that code give list of users and group which they not belong from one of towns..
To add the missing entries to user_has_role_group, you might want to have some mapping between those town names and their group_id's.
The example below is just using a subquery with unions for that.
But you could replace that with a select from a table.
Maybe even from role_group, if those names correlate with the user town names.
insert into user_has_role_group (user_id, group_id)
select u.user_id, g.group_id
from user u
join (
select 'Paris' as name, 1 as group_id union all
select 'Rome', 2
-- add more towns here
) g on (u.town = g.name)
left join user_has_role_group ug
on (ug.user_id = u.user_id and ug.role_group_id = g.group_id)
where u.town in ('Paris','Rome') -- add more towns here
and json_extract(u.custom_metadata, '$.age') = 18
and ug.id is null;

How to join any number of tables in MySQL?

I am having a major problem joining 5 tables because each table only has 1 column in common with only 1 other table.
Here are my tables and columns in each table:
TABLE (COLUMNS)
person (person_id, first_name, last name)
building (building_id, building_name)
room (room_id, room_number, building_id, capacity)
meeting (meeting_id, room_id, meeting_start, meeting_end)
person_meeting (person_id, meeting_id)
OK, now here is what I am trying to do (pasted from a homework assignment):
Construct the SQL statement to find all the meetings that person_id #1 has to attend. Display the following columns:
Person’s first name
Person’s last name
Building name
Room number
Meeting start date and time
Meeting end date and time
Now I know how to join 2 tables but I have no idea how to pull info from 5 different tables like this.
I tried looking up how to do this and it just says to do a UNION command, and I am just learning and have yet to cover that.
As UNION is used to combine the result from multiple SELECT statements into a single result set, you don't need it for this scenario. You have to join all the tables one by one based on their Id.
SELECT P.First_Name, P.Last_Name, B.Building_name, R.Room_Number,
M.Meeting_Start, M.Meeting_End FROM Person P
JOIN Person_Meeting PM ON P.Person_Id = PM.Person_Id
JOIN Meeting M ON PM.Meeting_Id = M.Meeting_Id
JOIN Room R ON M.Room_Id = R.Room_Id
JOIN Building B ON R.Building_Id = B.Building_Id
WHERE P.Person_Id = 1

Combine data from 2 "strange" tables

Having the following Schema
(ER of DataBase)
I am trying to create a query that
will show the title(Movies.Title) along with each movie's genre(movie_Genres.movie_genre) for each movie that..
that have been seen in (a) a specific time period (lets say 2-3 days for ex. We can take those days from the Tickets.ticket_date) (b) by Male Customers(Customer.customer_sec) and (c) got rated more than 4 (rated_customerRation) by those customers.
I can get as close as the following query:
SELECT
`movie_title`, `movie_Genres`.`movie_genre`
FROM
`Movies`
INNER JOIN
`movie_Genres` ON `mg_movie` = `movie_ID`
INNER JOIN
`Rated` ON `rated_movie_ID` = `movie_ID`
WHERE `rated_customerRatio` > 4 AND
UNION
(SELECT
`customer_sex`, `rated_customerRatio`
FROM
`Customer`
/* INNER JOIN
`cinema`.`Rated` ON `Rated`.`rated_customer_tabID` = `Customer`.`customer_tabID`
*/
INNER JOIN
`cinema`.`Tickets` ON `Tickets`.`ticket_customer_tabID`=`Customer`.`customer_tabID`
WHERE
`customer_sex` LIKE 'Male'
/* AND rated_customerRatio > 4 */
AND `Tickets`.`ticket_date` > '2016-02-16')
GROUP BY `movie_title`;
Also I am thinking that I will have trouble, cause one movie can have more than one Genre, and I don't want double lines, for the same movie, in my outcome.
Any help will be taken into serious regards!
use a WHERE movie.ID IN (Select movie.id...) query to have unusual query relationships. i.e. if movie.id can be related to tickets
WHERE MovieID IN
(
SELECT
MovieID
FROM
MovieTable
LEFT JOIN TICKETS ON MovieID = TICKETS.movieID
LEFT JOIN CUSTOMER ON TICKETS.ID = CUSTOMER.ID
WHERE
CUSTOMER.customer_sex LIKE 'Male'
AND Tickets.ticket_date > '2016-02-16')
)
I dont know your DB schema but that should give you a rough idea on one way of sorting based on viewership of moves from tickets? 1 ticket should have 1 movie ID and 1 customer, so the relationship is pretty easy to figure out

SQL query to get records that match ALL the criteria

I need to write a SQL query to get the patients that have stayed in ALL the hospitals of the city where they live. In one city there may be several hospitals of course.
So for example, if the patient 'xxx' who lives in Washington has been in a hospital, I need to list him only if he's been in all the hospitals of Washington and no less.
This is the structure of the tables:
table patient
patientID
patientCity
table hospital
hospitalCode
hospitalCity
table hospital_stay
hospitalCode
patientID
cityStay
What's the most efficient way to do this for MySQL? Thank you!
Unfortunately, MySQL can't order before grouping, so I had to use subquery to order the result correctly before grouping it.
Have fun :)
SELECT * FROM (
SELECT
p.patientID,
hs.hospitalCode
FROM
patient p
INNER JOIN hospital h ON (p.patientCity = h.hospitalCity)
LEFT JOIN hospital_stay hs ON (p.patientID = hs.patientID AND h.hospitalCode = hs.hospitalCode)
ORDER BY 2
) AS tmp_table
GROUP BY 1
HAVING NOT ISNULL(hospitalCode)
This query should work :
Select p.patientID
, p.patientCity
from patient p
inner join hospital h on h.hospitalCity = p.patientCity
inner join hospital_stay hs on hs.hospitalCode = h.hospitalCode
--where hs.cityStay = 1
group by p.patientID, p.patientCity
having count(*) = (select count(*) from hospital
where hospitalCity = p.patientCity);
Remove the comment if cityStay is kind of a flag that says that the patient went to the hospital.