Get team names on different tables!

Asked

Viewed 38 times

1

Good morning guys, once again I come to ask for a help.

I have 3 tables in BD to display name of the teams, location and competition, bringing the results with JOIN this working, but I’m not able to show the name 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 ( Lead Team - Visiting Team - Game Location )

Follow the example running http://sqlfiddle.com/#! 9/74e90e/1

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);

1 answer

2


Try this Marcos 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
  • Perfect, Subselects, always forget this feature, hehehe, thank you Ricardo, I will adapt to what I need here!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.