I have to do an exercise on SQL, i ve got two tables:
create table REPRESENTA (
cod_pais1 varchar (5),
cod_pais2 varchar (5),
primary key(cod_pais1,cod_pais2));
insert into representa values('p002','p003');
insert into representa values('p001','p004');
create table PAIS(
Cod_pais varchar (5)primary key unique,
nombre varchar (25) not null,
continente varchar (25)not null,
clubes numeric (9));
insert into pais values('p001','rusia','europa','10');
insert into pais values('p002','francia','europa','3');
insert into pais values('p003','guayana francesa','america','1');
insert into pais values('p004','uzbekistan','asia','8');
insert into pais values('p005','nigeria','africa','14');
I have to make a consult to show REPRESENTA tables as it is but with the names related from PAIS table. And the professor doesnt allow use subselects.
I tried with
select pais.nombre as A, pais.nombre as B from pais join representa R on
pais.Cod_pais=representa.cod_pais1 join representa on
pais.Cod_pais= representa.cod_pais2;
but it goes error if i use the same table twice.
I think you are looking for something like below.
select p1.nombre as a, p2.nombre as b
from representa R
join PAIS p1
on p1.Cod_pais = R.cod_pais1
join PAIS p2
on p2.cod_pais = R.cod_pais2
The idea here is if you want to show nombre
associted with one another in table representa
, you need to join table PAIS
twice to table representa
; 1 time for nombre
associated with cod_pais1
and 2nd time for nombre
associated with cod_pais2
.
Result:
+---------+------------------+
| a | b |
+---------+------------------+
| francia | guayana francesa |
| rusia | uzbekistan |
+---------+------------------+
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.