I have these tables :
TBL_PERSONS - a table for persons
TBL_SPORTS - a table for sports like football , basketball , tennis and ...
TBL_PERSON_SPORT - a table for any person's sports
for example Jo can play Football , and Basketball
I want to create a VIEW on persons and their sports.
something like this:
Jo|Football,Basketball
Jack|Football,Tennis
how should I write such VIEW?
thanks.
If you really want them to be a comma-separated list, that is the function of the GROUP_CONCAT() aggregate function. Substitute the correct names of your columns in place of name, person_id, sport_id, etc...
CREATE VIEW PERSON_SPORTS AS
(
SELECT
p.name,
GROUP_CONCAT(s.sport_name) AS sports
FROM
TBL_PERSONS p
LEFT JOIN TBL_PERSON_SPORT sp ON p.person_id = sp.person_id
JOIN TBL_SPORTS s ON sp.sport_id = s.sport_id
GROUP BY p.name
);
The above won't be very useful for joining against though, if you need to be able to separate out sports from the comma-separated list. Consider also just wrapping the ungrouped list in a view as:
CREATE VIEW PERSON_SPORTS AS
(
SELECT
DISTINCT
p.name,
s.sport_name AS sport
FROM
TBL_PERSONS p
LEFT JOIN TBL_PERSON_SPORT sp ON p.person_id = sp.person_id
JOIN TBL_SPORTS s ON sp.sport_id = s.sport_id
);
This will produce one row per sport, containing the person's name and sport name, where the person is duplicated as many times as needed for each sport.
Related
I have 2 tables:
Table 1 called teams: that contains different columns like Team_ID, Team_Name... etc.
table 2 called matches: that contains different columns like Match_ID, Home, Away, Match_Date.
Home and away are the names of 2 teams that I generated,
I tried different queries to generate a result where I have teams ID instead of teams names
for example:
SELECT t1.Home,t2.Away from
(SELECT a.Team_ID AS Home, b.Match_Date from teams a
INNER JOIN matches b ON a.Team_Name=b.Home
Where b.Match_Date="2020-05-29 23:59:59") t1,
(SELECT a.Team_ID AS Away, b.Match_Date from teams a
INNER JOIN matches b on a.Team_Name=b.Away
Where b.Match_Date="2020-05-29 23:59:59") t2;
But it didn't give me the result that I'm looking for
and After getting the result, I just to want filter it using a specific date like WHERE Match_Date= "date"
An image to clarify what I'm trying to do:
You want two joins on teams (one for the home team and the other for the away team), and a where clause to filter on the match date:
select th.team_id home, ta.team_id away, m.match_date
from matches m
inner join teams th on th.team_name = m.home
inner join teams ta on ta.team_name = m.away
where m.match_date = 'date1'
Note that you should not be storing the team name in matches, but the team id instead (which, presumably, is the primary key of teams).
There are two tables: Person, House
House has a FK to Person called person_id
House has a field called city
Is there a way to list all Person with a House in both city_a and city_b? This should exclude people with only houses in one of the city but include people who has houses in both and also additional cities.
This is my current query:
SELECT person.*
FROM Person person
JOIN House house ON house.person_id = person.id
WHERE house.city IN ("city_a", "city_b");
However, this query only returns list of people who have houses in either city_a or city_b, so it doesn't satisfy the AND condition.
A simple method uses exists . . . twice:
select p.*
from person p
where exists (select 1 from house h where h.person_id = p.id and h.city = 'city_a') and
exists (select 1 from house h where h.person_id = p.id and h.city = 'city_b') ;
If you just wanted the ids of the person, then group by and having are convenient:
select h.person_id
from house h
where h.city in ('city_a', 'city_b')
group by h.person_id
having count(distinct h.city) = 2
Is there a way in which I can change the way in which I see results of my query?
I'll use an example. I have a table with the id of a market, and then the name of the market, and the sport ID. I want the the sport id to be displayed as a sport name instead.
SELECT id, name, sport_id FROM markets where sport_id = 2;
I was thinking something like:
SELECT * FROM markets where sport_id = 2 as 'Football';
But that didn't work. I don't want to modify the results like an update would, I just want the results to be displayed as football instead of sport_id 2.
Is this possible?
if you do have only one table and like to give Alias then go for Query 1
SELECT id, name, sport_id AS 'sport name' FROM markets where sport_id = 2;
or
if you do have two different tables then Go for it
You need to join with other table as given below
SELECT m.id, m.name, t.sport_name
FROM markets m
JOIN other_Table t ON m.sport_id = t.sport_Id
where m.sport_id = 2;
I hope this might be helpful to solve your Issue.
You can do this with a JOIN to your sports table, something like:
SELECT m.id, m.name, s.sport_name
FROM markets m
JOIN sports s
ON m.sport_id = s.sport_Id
where m.sport_id = 2;
Hi
I have this three table . and i have form to put the id_student then the view must be :
the name + surname +father then
the subjects and the mark of each subjects
how i can do that ?
i try this :
select students.name, grade.marks
from students
INNER JOIN grade ON students.id_students = grade.id_students
You are almost there, you need to join with the third table as well. Considering you want all the column from 3 table do something like
select students.*,
grade.*,
Courses.*
from grade
INNER JOIN courses on grade.id_subject = courses.id_subject
INNER JOIN students ON students.id_students = grade.id_students
WHERE students.id_students=690
I'm sure this has been asked before but can't find the answer.
I have 3 tables OWNER, CAR, HOUSE
OWNER has 2 columns id and name
CAR has 3 columns id, ownerId and cartype
HOUSE has 4 columns id, ownerId, address, country
I want to write a SQL query that gets the owners name, cartypes, and addresses that are in Sweden
Here comes the tricky part. I want all the owners names and cartypes in the result table even if they don't own a house in Sweden. Can I get all that in 1 query or do I need to use 2? How would that query look?
You should be able to accomplish this with a simple left join:
SELECT O.name, C.cartype, H.address, H.country
FROM OWNER AS O
JOIN CAR AS C ON O.id = C.ownerid
LEFT JOIN HOUSE AS H ON O.id = H.ownerid AND Ucase(H.country) = "SWEDEN"
This will always give you a list of owners and their car types, in addition, it will give you a list of those that also happen to have a house address in sweden.
First you need to join the table then add new column in query by using CASE to check
SELECT o.* , c.* ,h.*,
(CASE WHEN h.county ='sweden' THEN h.county ELSE NULL END) AS HasCountry
FROM OWNER o
JOIN CAR c ON (c.ownerId =o.id)
JOIN HOUSE h ON (h.ownerId =o.id)