I would like to receive a data object country. An object country has the following attributes (id, continent_id, language_id).
Table countries {
id, name, continent_id, language_id
}
Table continents {
id, name
}
Table languages {
id, name
}
Instead of getting a DataObject containing only the continent_id and language_id, I want to get the name of the continent and the language. Like that:
{
id: 1,
name: Germany,
continent_id: 1,
language_id: 1,
continent: "europe", // new field
language: "german" // new field
}
How can I achieve this?
you need to join the two additional tables to the man countries
If every country has only one language, this will be enough to INNER JOIN them.
with multiply languages, you need to GROUP BY and use GROUP_CONCAT or the languages
SELECT
countr.id, contr.name, continent_id, language_id
, co.name as continent
, lang.name as language
FROM countries countr
INNER JOIN continents co ON contr.continent_id = co.id
INNER JOIN languages lang ON lang.id = contr.language_id
If a country has multiple languages
SELECT
countr.id, contr.name, continent_id, language_id
, co.name as continent
, GROUP_CONCAT(lang.name) as languages
FROM countries countr
INNER JOIN continents co ON contr.continent_id = co.id
INNER JOIN languages lang ON lang.id = contr.language_id
GROUP BY countr.id, contr.name, continent_id, language_id,co.name
I found a simple solution for my case. Maybe not the smartest but it works. There are probably better solutions. I don't know what influence the size of the data set will have on the performance. I still have to test this.
SELECT c.*, co.name as continent, l.name as language
FROM countries c
JOIN continents co ON co.id = c.continent_id
JOIN languages l ON l.id = c.language_id
WHERE c.id IN (1,2,3);
Related
By the title of the city (through a parametric window), withdraw the names of the most elderly vendors living in this city.
SELECT s.Name, s.Years, c.City
FROM Sellers AS s, City AS c, Shops AS sh
WHERE s.Years In (SELECT MAX(Years) FROM Sellers as s, City as c, Shops AS sh
WHERE s.CityID = c.CityID
AND City = Town AND c.City=Town
AND c.CityID=s.CityID);
First request doesn't work right.
By the name of the buyer (through a parametric window), withdraw the names of other buyers of its stores.
SELECT Name, Shop
FROM Buyers AS b, [Buyers shops] AS bs, Shops AS s
WHERE b.BuyersID = bs.BuyersID AND bs.ShopsID=s.ShopsID;
This is SQL communication between tables.
https://user-images.githubusercontent.com/77813063/112732441-aa238480-8f42-11eb-96f8-2d8d429f790e.jpg
enter image description here
Query#1
select s.Name, s.Years, c.City
from Sellers as s inner join City as c
on s.cityid=c.city_id
where years= (select max(years) from sellers as sel where sel.cityid=s.cityid)
Query#2
Please use proper join syntax instead of comma(,). This will make the query more readable and easy to modify at later point.
SELECT Name, Shop
FROM Buyers AS b inner join [Buyers shops] AS bs
on b.BuyersID = bs.BuyersID
inner join Shops AS s
on bs.ShopsID=s.ShopsID;
(a) List the names of the cities (from table City) whose name starts with 'p' and are in France (from table Country.
(b) List all columns from the Patient table for the patient's whose email contains '#gmail.com' and are from Atlanta
I have tried to join these tables but I do not seem know which values to join together
select city_name, email, first_name, last_name, pid, title, address_id, gender
from City c join Patient p on c.city_name = p.address_id
where p.email like '#gmail.com%'and c.city_name like 'Atlanta%
The result is supposed to show cities in France that start with 'p' and patients within a table whose email contains #gmail.com and that live in Atlanta.
You should not link the city name with the address id (c.city_name = p.address_id). Similar fields (usually primary and foreign keys) that contain the same structure (type, length, etc.) should be linked.
Since you are using the address ID, the address table must also be added as it contains the city ID. Regarding email search, "contains" means that it may be in the middle, so you must allow any characters at the beginning as well.
Try something like this:
select
c.city_name, p.email, p.first_name, p.last_name, pid, title, p.address_id, p.gender
from Patient as p
join Address as a on p.address_id = a.address_id
join City as c on c.city_id = a.city_id
where p.email like '%#gmail.com%'
and c.city_name = 'Atlanta'
I have three table which are cities, states and country. Each table contains its respective columns as shown below:
cities{id,cityname,states_id}
states{id,statename,country_id}
country{id,name}
Table Relations
cities table contains [states_id]
states table contain [country_id]
If I select particular city, I would need to display [cityname], [statename] and [countryname]
Based on what you provided:
SELECT ci.cityname
, st.statename
, cr.countryname
FROM Cities ci
JOIN States st ON ci.states_id = st.states_id
JOIN Country cr ON st.country_id = cr.country_id
SELECT cities.cityname, states.statename, countryname
FROM cities JOIN
states ON cities.states_id = states.id JOIN
country ON states.country_id = country.id
WHERE cities.id=3;
Assuming 3 is the id of the city you are searching for...
SELECT c.cityname, s.statename, cy.name AS countryname
FROM cities c
INNER JOIN states s ON s.id = c.states_id
INNER JOIN country cy ON cy.id = s.country_id
WHERE c.id = 123
"123" is the city id you want to be informed about.
I have these two tables:
CITY TABLE
CLUB TABLE
What I'm trying to do, is to select with the same query all cities that contain published clubs (published field set to 1) and the total of clubs published in that city.
At the moment, I am doing it with two steps, but I would like to improve performance by merging these in just one query.
SELECT c.id, c.name, c.slug
FROM city c, club cl
WHERE c.id = cl.city_id
AND ( SELECT COUNT(*)
FROM club cl, city c
WHERE cl.city_id = c.id AND cl.published = 1) > 0
GROUP BY c.id
After this, I'm doing a query for each city just to get the COUNT.
Something like this:-
SELECT city.id, city.name, city.slug, COUNT(club.id) AS club_count
FROM city
INNER JOIN club
ON city.id = club.city_id
WHERE club.published = 1
GROUP BY city.id, city.name, city.slug
HAVING club_count > 0
I have a created a table named city in my database. This table has 2 columns, called 'name' and 'country'. I have created a query that returns the combinations of cities from different countries which is below:
SELECT c1.name, c1.country, c2.name, c2.country
FROM city c1, city c2
WHERE c1.country != c2.country
This query works, but however the city pairs are repeated ie. I get results with:
Berlin Germany London England
London England Berlin Germany
which means that the city pair berlin/hamburg is repeated in my result set. Is there a way around this?
SELECT c1.name, c1.country, c2.name, c2.country
FROM city c1, city c2
WHERE c1.country < c2.country
You can do this by using LEFT JOIN see Visual Explanation Of Joins try this:
SELECT c1.name, c1.country, c2.name, c2.country
FROM city c1
LEFT JOIN city c2
ON c1.country = c2.country
AND c1.name = c2.name
WHERE c1.country IS NULL OR
c2.country IS NULL;
A good database will never have two or more same records in a single table. So my recommendation is two make 2 tables:
cities: ID, country_ID, Name
countries: ID, Name
and the select should be:
SELECT c.Name, co.Name
FROM cities c
INNER JOIN countries co
ON co.ID=c.country_ID