MySQL加入4个不同的表

时间:2013-05-23 15:11:11

标签: mysql join

如何加入这4个不同的表,这样我就可以获得字符串结果而不是数字。这是我目前的查询

SELECT mg_golub.ID AS IDgolub, mg_golub.brojgoluba, mg_golub.spol, 
       mg_golub.boja, mg_golub.rasa, mg_golub.ime, mg_golub.godina, 
       mg_drzava.drzava, mg_status.status
FROM mg_golub
JOIN mg_drzava 
  ON (mg_drzava.ID=mg_golub.IDdrzava)
JOIN mg_status 
  ON (mg_status.ID=mg_golub.IDstatus)
LEFT JOIN mg_popis_golubova AS Otac 
  ON (Otac.IDotac=mg_golub.ID)
LEFT JOIN mg_popis_golubova AS Majka 
  ON (Majka.IDmajka=mg_golub.ID)
JOIN mg_popis_golubova 
  ON (mg_golub.ID=mg_popis_golubova.IDgolub) 
WHERE mg_popis_golubova.IDkorisnik='$ID_KORISNIK' 

我不知道如何将这三个字段从mg_popis_golbova链接到mg_golub。我坚持,因为我无法为这两列选择数据。

LEFT JOIN mg_popis_golubova AS Otac ON (Otac.IDotac=mg_golub.ID)
LEFT JOIN mg_popis_golubova AS Majka ON (Majka.IDmajka=mg_golub.ID)

enter image description here

INSERT INTO `mg_golub` (`ID`, `IDkorisnik`, `brojgoluba`, `spol`, `boja`, `rasa`, `ime`, `godina`, `IDdrzava`, `IDstatus`) VALUES
(1, 1, '06557-07-681', 'X', 'kovana', 'Koopman x Schellens', 'Dirk', 2000, 2, 3),
(2, 1, '07216-05-552', 'X', 'kovana', 'Koopman', 'Tata', 2001, 2, 3),
(3, 1, '2081-01-271', 'Y', 'kovana', 'Schellens', 'Die Weischwantaubi', 2003, 2, 3);

INSERT INTO `mg_popis_golubova` (`ID`, `IDkorisnik`, `IDgolub`, `IDmajka`, `IDotac`) VALUES
(28, 1, 1, 3, 2),
(27, 1, 3, 0, 0),
(26, 1, 2, 0, 0);

1 个答案:

答案 0 :(得分:0)

试试这个

   SELECT mg_golub.ID AS IDgolub, mg_golub.brojgoluba, mg_golub.spol, 
       mg_golub.boja, mg_golub.rasa, mg_golub.ime, mg_golub.godina, 
       mg_drzava.drzava, mg_status.status
FROM mg_golub
JOIN mg_drzava 
  ON (mg_drzava.ID=mg_golub.IDdrzava)
JOIN mg_status 
  ON (mg_status.ID=mg_golub.IDstatus)
JOIN mg_popis_golubova 
  ON (mg_golub.ID=mg_popis_golubova.IDgolub) 
WHERE mg_popis_golubova.IDkorisnik='$ID_KORISNIK'