Klucz obcy / nazwa zamiast id

0

Witajcie, mam dwie bazy, których struktury wyglądają następująco

Tabela 1

create table producers(
id int unsigned not null,
name char(30) not null,
YoE int unsigned not null,
country char(20) not null,
website char(50) not null,
primary key(id))
engine = innodb
default character set utf8 collate utf8_unicode_ci;

Tabela 2

create table games(
id INT unsigned not null,
namegame char(100) not null,
publisher char(60) not null,
producer int unsigned,
type ENUM('action','strategy','party','logic','RPG','arcade','simulation','adventure','sport','racing','fighting'),
price double unsigned not null,
release_date date not null,
metacritics int unsigned not null,
exclusive ENUM('no','Wii','Switch','NDS','PSP','3DS','PS1','PSV','XBOX','GBA','PS2','PS3','PS4','X360','iOS','AND','GCN','PC'),
isSeries ENUM('YES','NO'),
pegi int unsigned not null,
primary key(id),
foreign key(producer) references producers(id))
engine = innodb;

Dodaje wartości do obu tabel

INSERT INTO producers
VALUES (1,'Ubisoft',1986,'France','www.ubi.com/');

INSERT INTO games
VALUES (1,'Far Cry 5','Ubisoft',1,'action',72.90,2018-03-27,78,'no','YES',18);

Oczywistością jest iż wpisując polecenie SELECT * FROM games; wyskakuje wiersz, w którym jest zapisane numer 1 w kolumnie producer. Jak napisać zapytanie aby zamiast tej "jedynki" była nazwa tego studia spod id=1 ? Dziękuję za wszelką pomoc <3

2

Musisz zrobić joina zdwoch tabl i wybrac to co cie interesuje

2

Takie coś?
SELECT *, producers.name FROM games JOIN producers ON producers.id = games.producer

0

Nie do końca, bo wyskakuje błąd :( xd

1

Moze nie badz taki tajmniczy i się podziel się błędem

0

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON games.producer = producers.id' at line 1

1

Po join powinna być nazwa tabeli producer i dopiero on

0

+----+-----------+-----------+----------+--------+-------+--------------+-------------+-----------+----------+------+----+---------+------+---------+--------------+---------+
| id | namegame | publisher | producer | type | price | release_date | metacritics | exclusive | isSeries | pegi | id | name | YoE | country | website | name |
+----+-----------+-----------+----------+--------+-------+--------------+-------------+-----------+----------+------+----+---------+------+---------+--------------+---------+
| 1 | Far Cry 5 | Ubisoft | 1 | action | 72.9 | 0000-00-00 | 78 | no | YES | 18 | 1 | Ubisoft | 1986 | France | www.ubi.com/ | Ubisoft |
+----+-----------+-----------+----------+--------+-------+--------------+-------------+-----------+----------+------+----+---------+------+---------+--------------+---------+

Wyświetliło takie coś a chciałbym by wyświetliło

+----+-----------+-----------+----------+--------+-------+--------------+-------------+-----------+----------+------+----+
| id | namegame | publisher | producer | type | price | release_date | metacritics | exclusive | isSeries | pegi
+----+-----------+-----------+----------+--------+-------+--------------+-------------+-----------+----------+------+----+
| 1 | Far Cry 5 | Ubisoft | Ubisoft | action | 72.9 | 0000-00-00 | 78 | no | YES | 18 |
+----+-----------+-----------+----------+--------+-------+--------------+-------------+-----------+----------+------+----+

0

Udało się xD
Po ciężkich bojach doszedłem do takiego czegoś
SELECT id_game, namegame,publisher,name as producer,type,price,release_date,metacritics,exclusive,isSeries,pegi FROM games INNER JOIN producers ON games.producer=producers.id_producer;
Na ten moment wystarczy :3

0

oo zaczynają się kampanie wrześniowe...
jak już zauważyłeś można sobie w SELECT dowolnie wybrać kolumny i zmieniać ich nazwy, polecam też dopisywanie aliasów do tabel czyli FROM games as g - bo potem wszędzie do tabeli odnosisz się g. a nie pełną nazwą i jak Cię wykładowca zapyta jak usunąć podwójne dane to pamiętaj o distinct

1 użytkowników online, w tym zalogowanych: 0, gości: 1