JOIN语句中的SQL问题

时间:2015-07-06 10:54:03

标签: mysql sql join left-join

我有这4张桌子:

CREATE TABLE IF NOT EXISTS `football_league` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `league` int(11) NOT NULL,
  `country` int(11) NOT NULL,
  `code` int(11) NOT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;



CREATE TABLE IF NOT EXISTS `football_goals` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `match` int(11) NOT NULL,
  `team` int(11) NOT NULL,
  `goals` int(11) NOT NULL,
  `time` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `match` (`match`),
  KEY `team` (`team`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;



CREATE TABLE IF NOT EXISTS `football_matches` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pcode` varchar(32) NOT NULL,
  `date` date NOT NULL,
  `time` time NOT NULL,
  `team_1` int(11) NOT NULL,
  `team_2` int(11) NOT NULL,
  `minutes` int(11) NOT NULL DEFAULT '0',
  `status` varchar(16) NOT NULL,
  `remarks` varchar(512) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `pcode` (`pcode`),
  KEY `team_1` (`team_1`),
  KEY `team_2` (`team_2`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;




CREATE TABLE IF NOT EXISTS `football_teams` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pcode` varchar(32) NOT NULL,
  `name` varchar(32) NOT NULL,
  `shortname` varchar(32) DEFAULT NULL,
  `played` int(11) NOT NULL,
  `won` int(11) NOT NULL,
  `draw` int(11) NOT NULL,
  `lost` int(11) NOT NULL,
  `points` int(11) NOT NULL,
  `previous_results` varchar(16) NOT NULL,
  `remarks` varchar(512) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `pcode` (`pcode`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;



--
-- Constraints for table `football_goals`
--
ALTER TABLE `football_goals`
  ADD CONSTRAINT `football_goals_ibfk_1` FOREIGN KEY (`match`) REFERENCES `football_matches` (`id`),
  ADD CONSTRAINT `football_goals_ibfk_2` FOREIGN KEY (`team`) REFERENCES `football_teams` (`id`);

--
-- Constraints for table `football_matches`
--
ALTER TABLE `football_matches`
  ADD CONSTRAINT `football_matches_ibfk_1` FOREIGN KEY (`pcode`) REFERENCES `football_league` (`code`),
  ADD CONSTRAINT `football_matches_ibfk_2` FOREIGN KEY (`team_1`) REFERENCES `football_teams` (`id`),
  ADD CONSTRAINT `football_matches_ibfk_3` FOREIGN KEY (`team_2`) REFERENCES `football_teams` (`id`);

--
-- Constraints for table `football_teams`
--
ALTER TABLE `football_teams`
  ADD CONSTRAINT `football_teams_ibfk_1` FOREIGN KEY (`pcode`) REFERENCES `football_league` (`code`);



INSERT INTO `football_league` (`id`, `league`, `country`, `code`) VALUES
(1, 'International', 'International', 'L001'),
(2, 'English Premier League', 'English', 'L002'),
(3, 'Scottish Premier League', 'Scotland', 'L003');


INSERT INTO `football_matches` (`id`, `pcode`, `date`, `time`, `team_1`, `team_2`, `minutes`, `status`, `remarks`) VALUES
(1, 'L001', '2015-07-06', '18:45:00', 1, 2, 0, 'running', '18:00'),
(2, 'L002', '2015-07-02', '18:00:00', 7, 8, 0, 'FT', ''),
(3, 'L001', '2015-07-06', '18:45:00', 1, 2, 22, 'running', '');



INSERT INTO `football_teams` (`id`, `pcode`, `name`, `shortname`, `played`, `won`, `draw`, `lost`, `points`, `previous_results`, `remarks`) VALUES
(1, 'L002', 'Arsenal', 'arsenal', 50, 30, 10, 10, 70, 'WWLLD', ''),
(2, 'L002', 'Chelsea', 'chelsea', 50, 20, 10, 20, 50, 'LLWWW', ''),
(6, 'L002', 'Manchester City', 'manchester', 30, 20, 3, 7, 60, 'WWWWW', ''),
(7, 'L001', 'England', 'england', 50, 20, 13, 17, 53, 'WLWLL', ''),
(8, 'L001', 'Brazil', 'brazil', 30, 22, 2, 6, 46, 'WLLWW', ''),
(9, 'L001', 'France', 'france', 36, 18, 10, 8, 46, 'LLLWW', '');


INSERT INTO `football_goals` (`id`, `match`, `team`, `goals`, `time`) VALUES
(1, 1, 1, 1, '2015-07-06 14:36:00'),
(2, 1, 2, 1, '2015-07-06 12:28:00'),
(3, 2, 7, 1, '2015-07-06 14:39:00'),
(4, 2, 8, 1, '2015-07-06 12:28:00'),
(6, 1, 1, 1, '2015-07-06 08:33:00');

我想写一个SQL语句,它将显示所有匹配特定联赛和日期的分数。我试着写下面的SQL:

SELECT 
football_league.code, football_matches.id,
football_league.league, football_league.country, 
football_matches.date, football_matches.time as match_time, 
football_matches.team_1, ft_1.name as name_a, 
ft_1.shortname as short_a,
football_matches.team_2, ft_2.name as name_b, 
ft_2.shortname as short_b, 
football_matches.minutes, football_matches.status, football_matches.remarks,
sum(fg_1.goals) as score_a,
sum(fg_2.goals) as score_b

FROM football_matches
INNER JOIN football_teams as ft_1 
ON ft_1.id = football_matches.team_1
INNER JOIN football_teams as ft_2 
ON ft_2.id = football_matches.team_2

INNER JOIN football_league 
ON football_league.code = football_matches.pcode

LEFT JOIN football_goals as fg_1 
ON `fg_1`.`match` = football_matches.id AND `fg_1`.`team` = ft_1.id

LEFT JOIN football_goals as fg_2 
ON `fg_2`.`match` = football_matches.id  AND `fg_2`.`team` = ft_2.id

WHERE 
football_league.code = 'L001' 
AND
football_matches.date = '2015-07-06'

我的问题是:

i)分数显示错误,应该是2-1,而不是显示2-2

ii)第二个问题是我还需要显示其他比赛,没有球队得分,没有显示,我尝试使用LEFT JOIN,但仍然没有显示其他比赛。

1 个答案:

答案 0 :(得分:2)

请尝试以下操作:

<h:panelGroup style="padding: 10px">
    <h:outputLabel value="Добавить #{cc.attrs.dynamicItem.label.toLowerCase()}"/>
</h:panelGroup>