1

为了使我的问题尽可能清晰(我之前因过于模糊而被抱怨),这是我在一个项目中使用的两个表的结构和数据。表 1 包含足球比赛列表,表 2 包含将在表 1 中记录的英国足球队。

我只是在从图书馆拿出的一本书中学习 SQL。我已按照有关连接表数据的说明进行操作。在这里,我只是尝试将数据回显到页面上,以确保我的查询在围绕该数据设置页面样式之前是正确的。

    <?php
        //Set DB Variables
        $dbc = mysql_connect(host, username, password);
        $db = mysql_select_db(database);
        $results= mysql_query("SELECT 'tbl_games.game_ID', 'tbl_games.game_date', 'tbl_teams.team_name' FROM tbl_teams, tbl_games
            WHERE 'tbl_games.team1_ID' = 'tbl_teams.team_ID' AND 'tbl_games.team2_ID' = 'tbl_teams.team_ID' AND 'tbl_games.team1_score' IS NULL AND 'tbl_games.team2_score' IS NULL");
        while ($row = mysql_fetch_array($results)) {
            foreach ($row as $columnName => $results) {
                echo 'Column name: '.$columnName.' Column data: '.$columnData.'<br/>';
            }
        }
    ?>

当我运行代码时,页面上没有打印任何错误,它只是不打印任何内容。但是有(或至少应该有)一些结果出现了。我在这里做错了什么?

--
-- Table structure for table `tbl_games`
--

DROP TABLE IF EXISTS `tbl_games`;
CREATE TABLE IF NOT EXISTS `tbl_games` (
  `game_ID` int(6) NOT NULL AUTO_INCREMENT,
  `team1_ID` int(4) NOT NULL,
  `team2_ID` int(4) NOT NULL,
  `team1_score` int(2) DEFAULT NULL,
  `team2_score` int(2) DEFAULT NULL,
  `game_date` date NOT NULL,
  PRIMARY KEY (`game_ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=121 ;

--
-- Dumping data for table `tbl_games`
--

INSERT INTO `tbl_games` (`game_ID`, `team1_ID`, `team2_ID`, `team1_score`, `team2_score`, `game_date`) VALUES
(1, 42, 34, NULL, NULL, '2013-08-23'),
(2, 159, 45, NULL, NULL, '2013-08-23'),
(3, 5, 122, NULL, NULL, '2013-08-23'),
(4, 67, 12, NULL, NULL, '2013-08-24'),
(5, 60, 155, NULL, NULL, '2013-08-24'),
(6, 78, 105, NULL, NULL, '2013-08-24'),
(7, 101, 156, NULL, NULL, '2013-08-24'),
(8, 134, 144, NULL, NULL, '2013-08-24'),
(9, 142, 47, NULL, NULL, '2013-08-24'),
(10, 13, 88, NULL, NULL, '2013-08-24'),
(11, 21, 120, NULL, NULL, '2013-08-24'),
(12, 19, 16, NULL, NULL, '2013-08-24'),
(13, 20, 123, NULL, NULL, '2013-08-24'),
(14, 26, 29, NULL, NULL, '2013-08-24'),
(15, 36, 51, NULL, NULL, '2013-08-24'),
(16, 77, 21, NULL, NULL, '2013-08-24'),
(17, 81, 84, NULL, NULL, '2013-08-24'),
(18, 85, 18, NULL, NULL, '2013-08-24'),
(19, 132, 96, NULL, NULL, '2013-08-24'),
(20, 162, 50, NULL, NULL, '2013-08-24'),
(21, 22, 131, NULL, NULL, '2013-08-24'),
(22, 25, 152, NULL, NULL, '2013-08-24'),
(23, 86, 46, NULL, NULL, '2013-08-24'),
(24, 97, 27, NULL, NULL, '2013-08-24'),
(25, 107, 140, NULL, NULL, '2013-08-24'),
(26, 109, 115, NULL, NULL, '2013-08-24'),
(27, 127, 133, NULL, NULL, '2013-08-24'),
(28, 146, 69, NULL, NULL, '2013-08-24'),
(29, 150, 112, NULL, NULL, '2013-08-24'),
(30, 2, 38, NULL, NULL, '2013-08-24');

-- --------------------------------------------------------

--
-- Table structure for table `tbl_teams`
--

DROP TABLE IF EXISTS `tbl_teams`;
CREATE TABLE IF NOT EXISTS `tbl_teams` (
  `team_ID` int(4) NOT NULL AUTO_INCREMENT,
  `team_name` varchar(50) COLLATE latin1_general_ci NOT NULL,
  PRIMARY KEY (`team_ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=164 ;

--
-- Dumping data for table `tbl_teams`
--

INSERT INTO `tbl_teams` (`team_ID`, `team_name`) VALUES
(1, 'Aberdeen'),
(2, 'Accrington Stanley'),
(3, 'AFC Bournemouth'),
(4, 'AFC Wimbledon'),
(5, 'Airdrieonians'),
(6, 'Albion Rovers'),
(7, 'Aldershot Town'),
(8, 'Alfreton Town'),
(9, 'Alloa Athletic'),
(10, 'Annan Athletic'),
(11, 'Arbroath'),
(12, 'Arsenal'),
(13, 'Aston Villa'),
(14, 'Ayr United'),
(15, 'Barnet'),
(16, 'Barnsley'),
(17, 'Berwick Rangers'),
(18, 'Birmingham City'),
(19, 'Blackburn Rovers'),
(20, 'Blackpool'),
(21, 'Bolton Wanderers'),
(22, 'Bradford City'),
(23, 'Braintree Town'),
(24, 'Brechin City'),
(25, 'Brentford'),
(26, 'Brighton & Hove Albion'),
(27, 'Bristol City'),
(28, 'Bristol Rovers'),
(29, 'Burnley'),
(30, 'Burton Albion'),
(31, 'Bury'),
(32, 'Cambridge United'),
(33, 'Cardiff City'),
(34, 'Carlisle United'),
(35, 'Celtic'),
(36, 'Charlton Athletic'),
(37, 'Chelsea'),
(38, 'Cheltenham Town'),
(39, 'Chester'),
(40, 'Chesterfield'),
(41, 'Clyde'),
(42, 'Colchester United'),
(43, 'Coventry City'),
(44, 'Cowdenbeath'),
(45, 'Crawley Town'),
(46, 'Crewe Alexandra'),
(47, 'Crystal Palace'),
(48, 'Dagenham & Redbridge'),
(49, 'Dartford'),
(50, 'Derby County'),
(51, 'Doncaster Rovers'),
(52, 'Dumbarton'),
(53, 'Dundee'),
(54, 'Dundee United'),
(55, 'Dunfermline'),
(56, 'East Fife'),
(57, 'East Stirlingshire'),
(58, 'Elgin City'),
(59, 'England'),
(60, 'Everton'),
(61, 'Exeter City'),
(62, 'Falkirk'),
(63, 'FC Halifax Town'),
(64, 'Fleetwood Town'),
(65, 'Forest Green Rovers'),
(66, 'Forfar Athletic'),
(67, 'Fulham'),
(68, 'Gateshead'),
(69, 'Gillingham'),
(70, 'Greenock Morton'),
(71, 'Grimsby Town'),
(72, 'Hamilton Academical'),
(73, 'Hartlepool United'),
(74, 'Heart of Midlothian'),
(75, 'Hereford United'),
(76, 'Hibernian'),
(77, 'Huddersfield Town'),
(78, 'Hull City'),
(79, 'Hyde'),
(80, 'Inverness Caledonian Thistle'),
(81, 'Ipswich Town'),
(82, 'Kidderminster Harriers'),
(83, 'Kilmarnock'),
(84, 'Leeds United'),
(85, 'Leicester City'),
(86, 'Leyton Orient'),
(87, 'Lincoln City'),
(88, 'Liverpool'),
(89, 'Livingston'),
(90, 'Luton Town'),
(91, 'Macclesfield Town'),
(92, 'Manchester City'),
(93, 'Manchester United'),
(94, 'Mansfield Town'),
(95, 'Middlesbrough'),
(96, 'Millwall'),
(97, 'Milton Keynes Dons'),
(98, 'Montrose'),
(99, 'Morecambe'),
(100, 'Motherwell'),
(101, 'Newcastle United'),
(102, 'Newport County'),
(103, 'Northampton Town'),
(104, 'Northern Ireland'),
(105, 'Norwich City'),
(106, 'Nottingham Forest'),
(107, 'Notts County'),
(108, 'Nuneaton Town'),
(109, 'Oldham Athletic'),
(110, 'Oxford United'),
(111, 'Partick Thistle'),
(112, 'Peterborough United'),
(113, 'Peterhead'),
(114, 'Plymouth Argyle'),
(115, 'Port Vale'),
(116, 'Portsmouth'),
(117, 'Preston North End'),
(118, 'Queen of the South'),
(119, 'Queen''s Park'),
(120, 'Queens Park Rangers'),
(121, 'Raith Rovers'),
(122, 'Rangers'),
(123, 'Reading'),
(124, 'Republic of Ireland'),
(125, 'Rochdale'),
(126, 'Ross County'),
(127, 'Rotherham United'),
(128, 'Salisbury City'),
(129, 'Scotland'),
(130, 'Scunthorpe United'),
(131, 'Sheffield United'),
(132, 'Sheffield Wednesday'),
(133, 'Shrewsbury Town'),
(134, 'Southampton'),
(135, 'Southend United'),
(136, 'Southport'),
(137, 'St Johnstone'),
(138, 'St Mirren'),
(139, 'Stenhousemuir'),
(140, 'Stevenage'),
(141, 'Stirling Albion'),
(142, 'Stoke City'),
(143, 'Stranraer'),
(144, 'Sunderland'),
(145, 'Swansea City'),
(146, 'Swindon Town'),
(147, 'Tamworth'),
(148, 'Torquay United'),
(149, 'Tottenham Hotspur'),
(150, 'Tranmere Rovers'),
(151, 'Wales'),
(152, 'Walsall'),
(153, 'Watford'),
(154, 'Welling United'),
(155, 'West Bromwich Albion'),
(156, 'West Ham United'),
(157, 'Wigan Athletic'),
(158, 'Woking'),
(159, 'Wolverhampton Wanderers'),
(160, 'Wrexham'),
(161, 'Wycombe Wanderers'),
(162, 'Yeovil Town'),
(163, 'York City');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
4

4 回答 4

3

查询中的那些单引号'(在 PHP 代码中)应该是反引号`,就像它们在创建表的语句中一样。

否则,您在where子句中比较的文字字符串不匹配。

而且,顺便说一句,除非绝对必要,否则我不太喜欢使用反引号。在我看来,这通常会使 SQL 语句的可读性降低。当然,它有它的位置,有些人可能更喜欢一致性而不是可读性,但我不是其中之一 :-)

但是,即使您解决了这个问题,您也会遇到以下问题:

WHERE tbl_games.team1_ID = tbl_teams.team_ID
  AND tbl_games.team2_ID = tbl_teams.team_ID

因为你似乎在要求所有那些球队与自己交手的比赛,我在很多体育比赛中都没有看到过。

您需要重新考虑您的要求。例如,如果您只是想要一个团队参加的所有游戏(作为团队 1 或 2),您将使用 OR 而不是 AND(并且还使用括号来确保 AND/OR 的正确混合)。

于 2013-08-24T21:17:18.867 回答
0
        $results= mysql_query("SELECT 'tbl_games.game_ID', 'tbl_games.game_date', 'tbl_teams.team_name' FROM tbl_teams, tbl_games
        WHERE 'tbl_games.team1_ID' = 'tbl_teams.team_ID' AND 'tbl_games.team2_ID' = 'tbl_teams.team_ID' AND 'tbl_games.team1_score' IS NULL AND 'tbl_games.team2_score' IS NULL");

应该:

        $results= mysql_query("SELECT `tbl_games`.`game_ID`, `tbl_games`.`game_date`, `tbl_teams`.`team_name` FROM tbl_teams, tbl_games
        WHERE `tbl_games`.`team1_ID` = `tbl_teams`.`team_ID` AND `tbl_games`.`team2_ID` = `tbl_teams`.`team_ID` AND `tbl_games`.`team1_score` IS NULL AND `tbl_games`.`team2_score` IS NULL");

您使用了错误的引号类型,并且.不能在引号内。

由于您的表名或列名都不是保留字,因此您可以完全不使用引号:

        $results= mysql_query("SELECT tbl_games.game_ID, tbl_games.game_date, tbl_teams.team_name FROM tbl_teams, tbl_games
        WHERE tbl_games.team1_ID = tbl_teams.team_ID AND tbl_games.team2_ID = tbl_teams.team_ID AND tbl_games.team1_score IS NULL AND tbl_games.team2_score IS NULL");

我认为您可能想要的实际查询是:

SELECT tbl_games.game_ID, tbl_games.game_date, tbl_teams.team_name
FROM tbl_teams, tbl_games
WHERE (tbl_teams.team_ID = tbl_games.team1_ID AND tbl_games.team1_score IS NULL)
OR (tbl_teams.team_ID = tbl_games.team2_ID AND tbl_games.team2_score IS NULL)

这会找到每支球队计划参加的所有比赛,并且他们的得分为零。

SQLFIDDLE

于 2013-08-24T21:19:40.653 回答
0

可能的原因:它正在从 2 个表(tbl_teams 和 tbl_games)中选择数据
条件是 tbl_teams.team_ID 等于 team2_IDteam1_ID。除非有比赛双方都是同一支球队,否则这永远不会奏效。

您没有指定要查询返回的内容,因此我假设您只想要两个团队的结果。

您可能想要的是一个涉及 3 组数据的连接:游戏、一队、二队。在 SQL 中,这可以使用别名来完成。

尝试这个:

SELECT tbl_games.game_ID, 
       tbl_games.game_date,
       team1.team_name as "team1_name",
       team2.team_name as "team2_name"
FROM 
    tbl_teams as team1, 
    tbl_teams as team2, 
    tbl_games
WHERE 
    tbl_games.team1_ID = team1.team_ID AND 
    tbl_games.team2_ID = team2.team_ID AND
    tbl_games.team1_score IS NULL AND 
    tbl_games.team2_score IS NULL;

添加“is null”将减少结果。如果所有游戏都有分数,您将不会得到任何结果。

您的查询的另一个潜在问题是字段名称在单引号之间。在实际执行查询之前,我不确定这些是否被 php 删除。如果不是,那就不好了;单引号之间的任何内容都被视为字符串。这意味着条件'tbl_games.team1_score' IS NULL实际上是将字符串 'tbl_games.team1_score' 与 null 进行比较,而不是名为 team1_score 的字段的内容。

编辑:将 is NULL 添加到 where 子句中,就像在示例数据中您确实有空值一样。

于 2013-08-24T21:33:58.590 回答
0

我认为您的查询的问题是在这种情况下'tbl_games.team1_ID' = 'tbl_teams.team_ID' AND 'tbl_games.team2_ID' = 'tbl_teams.team_ID'。您正在将相同的 id 与两个不同的字段 tbl_games.team1_ID&进行比较tbl_games.team2_ID。我认为tbl_games.team1_ID是一个团队打vstbl_games.team2_ID这意味着tbl_games.team1_ID != tbl_games.team2_ID。所以你只需要两个团队中的任何一个。您的查询可能如下所示。

<?php
        //Set DB Variables
        $dbc = mysql_connect(host, username, password);
        $db = mysql_select_db(database);
        $results= mysql_query("SELECT tbl_games.game_ID, tbl_games.game_date, tbl_teams.team_name FROM tbl_teams, tbl_games
            WHERE 'tbl_games.team1_ID' = 'tbl_teams.team_ID'  AND 'tbl_games.team1_score' IS NULL AND 'tbl_games.team2_score' IS NULL");
        while ($row = mysql_fetch_array($results)) {
            foreach ($row as $columnName => $results) {
                echo 'Column name: '.$columnName.' Column data: '.$columnData.'<br/>';
            }
        }
    ?>
于 2013-08-24T22:06:02.333 回答