I have the following tables:
flights: id, idcompany, idplane, fromCity, toCity, takeoff...
companies: id, name
planes: id, name
cities: id, name
I want to declare the names of the two cities "fromCity, toCity" in the same query.
MY purpose of this action is get the cities names at once, so I can display them to the client, without any need to do another query to get the cities names.
here is my try:
SELECT f.id, f.takeoff, f.arrival, ct.name as fromCity, f.toCity, c.name as company, p.name as plane
FROM flights f, companies c, planes p, cities ct
WHERE f.idCompany = c.id AND f.idPlane = p.id AND f.fromCity = ct.id
ORDER BY f.takeoff ASC
this query will return the name of the "fromCity" but the id of the "toCity", so what I can do to get the two names by the same query?
You could use this:
SELECT
f.id, f.takeoff, f.arrival, ct1.name as fromCity
,ct2.name as toCity, c.name as company, p.name as plane
FROM
flights f
inner join
companies c
on
f.idCompany = c.id
inner join
planes p
on
f.idPlane = p.id
inner join
cities ct1
on
f.fromCity = ct1.id
inner join
cities ct2
on
f.toCity = ct2.id
ORDER BY
f.takeoff ASC;
And try not using old-style-join from now on.
You have to join the table twice.
SELECT f.id, f.takeoff, f.arrival, ct.name as fromCity, f.toCity, c.name as company, p.name as plane , cf.name
FROM flights f, companies c, planes p, cities ct,cities cf
WHERE f.idCompany = c.id AND f.idPlane = p.id AND f.fromCity = ct.id AND f.toCity = cf.id
ORDER BY f.takeoff ASC
my solution is to make two copies of the table cities:
SELECT f.id, f.takeoff, f.arrival, ct1.name as fromCity, ct2.name as toCity, c.name as company, p.name as plane
FROM flights f, companies c, planes p, cities ct1, cities ct2
WHERE f.idCompany = c.id AND f.idPlane = p.id AND f.fromCity = ct1.id AND ct2.id = f.toCity
ORDER BY f.takeoff ASC
Related
EDIT:
With help from '#juergen d' I was finally able to create this query.
SELECT p.name as Product, c.name as Company, r.name as Reseller
FROM products p
INNER JOIN companies c ON c.id = p.company_id
INNER JOIN resellers_match rm ON c.id = rm.reseller_id
INNER JOIN reseller r ON rm.reseller_id = r.id
ORDER BY p.name
ORIGINAL:
Title: Select from multiple tables in MySQL join? group by?
I have 4 tables:
1- resellers [id, name, etc..]
2- resellers_match [id, reseller_id, company_id]
3- companies [id, name, etc..]
4- products [id, name, company_id, etc..]
I need to select * from products where company_id matches company_id.reseller_match and reseller.reseller_match = x;
I not being able to find how I can do that. Thank you.
SELECT p.name as Product, c.name as Company, r.name as Reseller
FROM products p
INNER JOIN companies c ON c.id = p.company_id
INNER JOIN resellers_match rm ON c.id = rm.reseller_id
INNER JOIN reseller r ON rm.reseller_id = r.id
ORDER BY p.name
I'm working with the Sakila sample database, and trying to get the most viewed film per country. So far I've managed to get the most viewed film of a certain country given its id with the following query:
SELECT
F.title, CO.country, count(F.film_id) as times
FROM
customer C
INNER JOIN
address A ON C.address_id = A.address_id
INNER JOIN
city CI ON A.city_id = CI.city_id
INNER JOIN
country CO ON CI.country_id = CO.country_id
INNER JOIN
rental R ON C.customer_id = R.customer_id
INNER JOIN
inventory I ON R.inventory_id = I.inventory_id
INNER JOIN
film F ON I.film_id = F.film_id
WHERE
CO.country_id = 1
GROUP BY
F.film_id
ORDER BY
times DESC
LIMIT 1;
I supose that I'll have to use this query or something similar in the FORM of another query, but I've tried it all I could think and am completely unable to figure out how to do so.
Thanks in advance!
I admit, this is a hell of a query. But well, as long as it works.
Explanation:
Subquery: almost the same as you already has. Without the WHERE and LIMIT. Resulting in a list of movie-count per country
Result of that, grouped per country
GROUP_CONCAT(title ORDER BY times DESC SEPARATOR '|||'), will give ALL titles in that 'row', with the most-viewed title first. The separator doesn't matter, as long as you are sure it will never occurs in a title.
SUBSTRING_INDEX('...', '|||', 1) results in the first part of the string until it finds |||, in this case the first (and thus most-viewed) title
Full query:
SELECT
country_name,
SUBSTRING_INDEX(
GROUP_CONCAT(title ORDER BY times DESC SEPARATOR '|||'),
'|||', 1
) as title,
MAX(times)
FROM (
SELECT
F.title AS title,
CO.country_id AS country_id,
CO.country AS country_name,
count(F.film_id) as times
FROM customer C INNER JOIN address A ON C.address_id = A.address_id
INNER JOIN city CI ON A.city_id = CI.city_id
INNER JOIN country CO ON CI.country_id = CO.country_id
INNER JOIN rental R ON C.customer_id = R.customer_id
INNER JOIN inventory I ON R.inventory_id = I.inventory_id
INNER JOIN film F ON I.film_id = F.film_id
GROUP BY F.film_id, CO.country_id
) AS count_per_movie_per_country
GROUP BY country_id
Proof of concept (as long as the subquery is correct): SQLFiddle
Hi I want to show the county in the following query but if county does not exist, return the city instead.
SELECT P.id AS id, SUM(P.price) AS price,
P.tax,
county
FROM Purchases AS P
JOIN Status AS S ON S.id = T.status_id
JOIN Shipping AS Ship ON S.shipping_id= Ship.id
JOIN Shipping_Addresses AS SA ON Ship.shipping_address_id = SA.id
JOIN Zip_Codes AS ZIP ON ZIP.zip_code = SA.zip_code
JOIN Counties AS C ON ZIP.city = C.city
JOIN States AS STATE ON STATE.id = C.county_state_id
GROUP BY ZIP.zip_code
ORDER BY county
Thanks
Try using COALESCE():
COALESCE(county, city) AS location
If county is NULL it will return the value of city instead.
So:
SELECT P.id AS id, SUM(P.price) AS price,
P.tax,
COALESCE(county, city) AS location
I have a MySQL database with two tables in it:
Members
- id, name, city_id
and:
Cities
- id, name
I want a query that will return each city name and how many members are in it.
The output I need is:
city_name count
------------------
New York 15
Los Angeles 20
something like this:
SELECT a.name, COUNT(DISTINCT b.city_id)
FROM Cities a JOIN Members b
ON b.city_id = a.id
GROUP BY b.city_id
SELECT
c.name AS city_name,
COUNT(m.id) AS counter
FROM Members m
JOIN Cities c ON m.city_id = c.id
select
cities.name as city_name,
count(cities.id) as counter
from
members inner join cities
on
members.city_id = cities.city.id
Try this
SELECT c.name city_name, COUNT(m.id) counter
FROM Cities c INNER JOIN Members m
ON c.id = m.city_id
GROUP BY c.name
Try something like this:
select city.name, count(member.id)
from city
inner join members
on city.id = members.city_id
group by city.name
Hope this helps.
I currently have the following:
Table Town:
id
name
region
Table Supplier:
id
name
town_id
The below query returns the number of suppliers for each town:
SELECT t.id, t.name, count(s.id) as NumSupplier
FROM Town t
INNER JOIN Suppliers s ON s.town_id = t.id
GROUP BY t.id, t.name
I now wish to introduce another table in to the query, Supplier_vehicles. A supplier can have many vehicles:
Table Supplier_vehicles:
id
supplier_id
vehicle_id
Now, the NumSupplier field needs to return the number of suppliers for each town that have any of the given vehicle_id (IN condition):
The following query will simply bring back the suppliers that have any of the given vehicle_id:
SELECT * FROM Supplier s, Supplier_vehicles v WHERE s.id = v.supplier_id AND v.vehicle_id IN (1, 4, 6)
I need to integrate this in to the first query so that it returns the number of suppliers that have any of the given vehicle_id.
SELECT t.id, t.name, count(s.id) as NumSupplier
FROM Town t
INNER JOIN Suppliers s ON s.town_id = t.id
WHERE s.id IN (SELECT sv.supplier_id
FROM supplier_vehicles sv
WHERE sv.vehicle_id IN (1,4,6))
GROUP BY t.id, t.name
Or you could do an INNER JOIN (as your supplier join is INNER, but this will remove towns with no suppliers with those vehicles) and change the COUNT(s.id) TO COUNT(DISTINCT s.id)
If I remember correctly, you can put your second query inside the LEFT OUTER JOIN condition.
So for example, you can do something like
...
LEFT OUTER JOIN (SELECT * FROM Suppler s, Supplier_vehicles ......) s ON s.town_id=t.id
In that way you are "integrating" or combining the two queries into one. Let me know if this works.
SELECT t.name, count(s.id) as NumSupplier
FROM Town t
LEFT OUTER JOIN Suppliers s ON t.id = s.town_id
LEFT OUTER JOIN Supplier_vehicles v ON s.id = v.supplier_id
WHERE v.vehicle_id IN (1,4,6)
GROUP BY t.name