This question already has answers here:
How to join two columns to the same table [duplicate]
(3 answers)
Closed 5 years ago.
I want to join 2 column that share same foreign key in another table
here's the tables:
country:
idcountry| countryname
1 german
2 america
destination
id|fromcountry |tocountry
1 1 2
the result that i wanted to:
id|fromc |toc
1 german america
Use simple Left Join ans provide alias to country name column:
SELECT d.idcountry as id, cf.CountryName as fromc, ct.CountryName as toc
FROM destination d
LEFT JOIN country cf ON d.fromcountry = cf.idcountry
LEFT JOIN country ct ON d.tocountry = ct.idcountry
Use Left Outer Join for both fields
something like this
SELECT Dest.ID, CFrom.CountryName, CTo.CountryName
FROM Destination Dest
LEFT OUTER JOIN Country CFrom ON Dest.FromCountry = CFrom.idcountry
LEFT OUTER JOIN Country CTo ON Dest.ToCountry = CTo.idcountry
Related
This question already has answers here:
mysql query show multiple tables from one ID column
(3 answers)
Closed 4 months ago.
I have tables like below,
Match table
date
team1
team2
2/2/2019
t001
t002
3/2/2019
t007
t002
Team table
teamID
teamName
t001
ABCD
t002
EFGH
t003
IJKL
t007
MNOP
I want to display Match table with teamName of team1 and team2 instead of teamID. I know I can join it as,
SELECT m.date, t.teamName, t.team2
FROM Match m INNER JOIN Team t
ON t.team1 = t.teamID;
But it can join with only one column. I don't know how to join both columns. Is there anyway to do this.
SELECT m.date, t.teamName, t1.teamName
FROM Match m INNER JOIN Team t
ON m.team1 = t.teamID
FROM Match m1 INNER JOIN Team t1
ON m1.team2 = t1.teamID;
or
SELECT m.date, t.teamName, t1.teamName
FROM Match m
INNER JOIN Team t ON m.team1 = t.teamID
INNER JOIN Team t1 ON m.team2 = t1.teamID;
It's the same
I don't understand the table name, why is a d.team2 if there is no alias d,. And why are team1 and team2 both also from table d.
But if I veratanden your two tables correctly the bottom query works.
But for multible joins you can just use different alias.
This question already has answers here:
Why does using a subquery inside a left join give a completely different answer than an equivalent table?
(2 answers)
Access 2007 - Left Join to a query returns #Error instead of Null
(3 answers)
Closed 1 year ago.
I have two tables in Access:
Table1:
ID
1 A
2 B
3 C
Table2:
ID
1 A
2 B
3 D
and a query (Query1) that adds a new column to Table 1:
SELECT [Table1].ID
,"NEW CATEGORY" AS Category
FROM [Table1]
Which looks like this:
ID Category
1 A NEW CATEGORY
2 B NEW CATEGORY
3 C NEW CATEGORY
Now, if I join Query1 to Table2, like this:
SELECT [Table2].ID
,[Query1].ID
,[Query1].Category
FROM [Table2]
LEFT JOIN [Query1] ON ([Table2].ID = [Query1].ID)
I get the following result:
Table2.ID Query1.ID Category
1 A A NEW CATEGORY
2 B B NEW CATEGORY
3 D NEW CATEGORY
"NEW CATEGORY" magically appears in row #3 in the Category column, whereas I am expecting an empty cell there.
Any ideas of the source of this strange behavior, or how it can be avoided?
id location code
1 Bangalore BN
2 Chennai CH
3 Kochi KH
slno first_loc Second_loc
1 1 2
3 3 1
How to join this two tables in MySQL
Just use an alias, I assume your first table named location, the second table named route since you don't give the tables' name
Select slno, a.location, b.location from route
left join location as a on route.first_loc = a.id
left join location as b on route.second_loc = b.id
This question already has answers here:
MySQL pivot row into dynamic number of columns
(1 answer)
MySQL - Rows to Columns
(13 answers)
Closed 5 years ago.
I have tree tables:
Persons:
id
name
Books
id
title
Quantity:
People_id
Product_id
quantity
I need a result with :
in the columns the Book title, in the rows the Persons name, in the cells the quantity take from the cross of peoples and books
select *
from persons p join quantity q on p.id = q.people_id
join books b on q.product_id = b.id
JOIN should help (if you need columns from all table then add alias.* for other tables in SELECT).
SELECT p.*
FROM persons p
JOIN quantity q ON p.id = q.people_id
JOIN books b ON q.product_id = b.id
I am planning to create a website similar to IMDB.com. To reduce execution time I am using the following structure. Is it okay for faster working?
Table - 1
Id Movie_name description
1 name one some description
2 name two some description
3 name three some description
Table 2
id actorname
1 name 1
2 name 2
3 name 3
4 name 4
Table 3
id movieid actorid
1 1 1
2 1 2
3 1 3
4 1 9
5 2 6
6 2 5
7 2 8
8 2 1
When I want to list actors in a movie program will retrieve actors ids from table 3 and find respective names from table 2 (using single query). When I want to list the movies of a actor it will retrieve movie ids from table 3 and find respective names from first table. Will it work properly? Any other ideas?
This will give all actors in a specified movie,
SELECT c.ID, c.actorName
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE a.ID = 1
This one will give all movies for a specified actor
SELECT a.*
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE c.ID = 1
SQLFiddle Demo (both queries)
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
UPDATE 1
This is called Relational Division
SELECT a.ID, a.Movie_Name
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE c.ID IN (1, 2, 3)
GROUP BY a.ID, a.Movie_Name
HAVING COUNT(DISTINCT c.ID) = 3
SQL of Relational Division
I suggest that you modify table3 by taking away the id field. Use the movieid and actorid together as your primary key. You might want to add other fields to this table such as name of character and order of appearance as suggested in the comment by Jermaine Xu.