Get names of teams in different tables!

1

Good morning, once again I ask for help.

I have 3 tables in the DB to display the teams name, location and the competition, bringing the results with JOIN working, but I am not able to show the names of the two teams in the result.

SELECT * FROM tabela JOIN times ON id_times = mandante_tabela JOIN locais ON id_locais = local_tabela

I need you to bring me (Time principal - Visiting Time)

Follow the example by running link

CREATE TABLE IF NOT EXISTS 'tabela' (
  'id_tabela' int(11) unsigned NOT NULL,
  'mandante_tabela' int(1) NOT NULL,
  'visitante_tabela' int(1) NOT NULL,
  'data_tabela' date NOT NULL,
  'hora_tabela' time NOT NULL,
  'local_tabela' int(11) NOT NULL,
  'placar_mandante_tabela' int(11) NOT NULL,
  'placar_visitante_tabela' int(11) NOT NULL,
  'ativa_tabela' int(1) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

--
-- Extraindo dados da tabela 'tabela'
--

INSERT INTO 'tabela' ('id_tabela', 'mandante_tabela', 'visitante_tabela', 'data_tabela', 'hora_tabela', 'local_tabela', 'placar_mandante_tabela', 'placar_visitante_tabela', 'ativa_tabela') VALUES
(8, 11, 9, '2016-07-13', '14:30:00', 8, 0, 0, 0),
(9, 9, 8, '2016-08-12', '11:30:00', 10, 0, 0, 0);

--
-- Indexes for dumped tables
--

--
-- Indexes for table 'tabela'
--
ALTER TABLE 'tabela'
  ADD UNIQUE KEY 'id_tabela' ('id_tabela');


CREATE TABLE IF NOT EXISTS 'locais' (
  'id_locais' int(11) unsigned NOT NULL,
  'nome_locais' varchar(100) NOT NULL,
  'ativa_locais' int(1) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;

--
-- Extraindo dados da tabela 'locais'
--

INSERT INTO 'locais' ('id_locais', 'nome_locais', 'ativa_locais') VALUES
(8, 'Mineirão', 0),
(9, 'Independência ', 0),
(10, 'Ipatinga', 0);


CREATE TABLE IF NOT EXISTS 'times' (
  'id_times' int(11) unsigned NOT NULL,
  'nome_times' varchar(100) NOT NULL,
  'ativa_times' int(1) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;

--
-- Extraindo dados da tabela 'times'
--

INSERT INTO 'times' ('id_times', 'nome_times', 'ativa_times') VALUES
(8, 'Riachinho', 0),
(9, 'Cruzeiro', 0),
(10, 'América', 0),
(11, 'Atlético', 0);
    
asked by anonymous 07.07.2016 / 16:00

1 answer

2

Try this Mark Paulo:

SELECT locais.nome_locais as  Local_do_Jogo,
(SELECT times.nome_times FROM times where times.id_times = tabela.mandante_tabela) as mandante,
(SELECT times.nome_times FROM times where times.id_times = tabela.visitante_tabela) as visitante
FROM tabela 
INNER JOIN locais ON locais.id_locais = tabela.local_tabela
    
07.07.2016 / 16:28