为什么我的查询只返回一行?

时间:2013-09-03 08:08:32

标签: php mysql select

在这种情况下我有三个主要表:leage_ladders,league_squads和league_experience。我目前正在进行阶梯视图页面,并希望找到具有经验的给定阶梯上的所有小队。查询不会返回任何错误,但是,它只返回一行,我不知道为什么。

以下是我正在使用的查询:

$query_squads = "
            SELECT
                s.squad_id AS squad_id, s.ladder_id, s.team_id AS team_id,
                x.experience_id, x.squad_id, SUM(x.value) as total_exp
            FROM league_squads AS s
            LEFT JOIN league_experience AS x ON (s.squad_id = x.squad_id)
            WHERE s.ladder_id = ".$ladder_id."
            HAVING total_exp > 0
            ORDER BY total_exp DESC
            ";

以下是我的表格:

--
-- Table structure for table `league_experience`
--

CREATE TABLE IF NOT EXISTS `league_experience` (
  `experience_id` int(15) NOT NULL,
  `squad_id` int(15) NOT NULL,
  `value` int(15) NOT NULL,
  `date_earned` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `description` varchar(255) NOT NULL,
  PRIMARY KEY (`experience_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `league_experience`
--

INSERT INTO `league_experience` (`experience_id`, `squad_id`, `value`, `date_earned`, `description`) VALUES
(1, 1, 500, '2013-09-03 07:10:59', 'For being ballers.'),
(2, 2, 250, '2013-09-03 07:10:52', 'For being awesome.');

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

--
-- Table structure for table `league_squads`
--

CREATE TABLE IF NOT EXISTS `league_squads` (
  `squad_id` int(15) NOT NULL AUTO_INCREMENT,
  `team_id` int(15) NOT NULL,
  `ladder_id` int(15) NOT NULL,
  `date_joined` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `status` tinyint(1) NOT NULL,
  `last_rank` tinyint(5) NOT NULL,
  PRIMARY KEY (`squad_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `league_squads`
--

INSERT INTO `league_squads` (`squad_id`, `team_id`, `ladder_id`, `date_joined`, `status`, `last_rank`) VALUES
(1, 1, 1, '2013-09-02 09:43:53', 0, 0),
(2, 2, 1, '2013-09-03 06:14:49', 0, 0);

当访问ladder_id 1时,查询应返回两个结果。但它只显示第一个结果。

4 个答案:

答案 0 :(得分:0)

您在选择部分中有汇总功能:SUM(x.value) as total_exp。因为没有GROUP BY,所以你会得到一行,其中total_exp是匹配行的总和。

答案 1 :(得分:0)

因为您使用的是不带SUM()子句的聚合函数GROUP BY。它的作用是计算所有过滤记录的总和,并返回非聚合列的随机值。尝试添加GROUP BY子句,

WHERE ...
GROUP BY s.squad_id, s.ladder_id, s.team_idd, x.experience_id, x.squad_id
ORDER BY ...

根据MySQL Docs,

  

如果在不包含GROUP BY的语句中使用组函数   子句,它相当于对所有行进行分组。更多   信息,请参阅Section 12.15.3, MySQL Extensions to GROUP BY

答案 2 :(得分:0)

这是因为您使用聚合函数:

SUM(x.value) as total_exp

自动分组行。 并且因为您没有定义GROUP BY子句,所以它将所有行组合在一起。

答案 3 :(得分:0)

是的,您使用了SUM()聚合函数。

我修复了你的SQL。

SELECT
s.squad_id AS squad_id, s.ladder_id, s.team_id AS team_id,
x.experience_id, x.squad_id, SUM(x.value) as total_exp
FROM league_squads AS s
LEFT JOIN league_experience AS x ON (s.squad_id = x.squad_id)
WHERE s.ladder_id = "1"
GROUP BY s.squad_id, s.ladder_id, s.team_id, x.experience_id
ORDER BY s.squad_id DESC

我只插入1行。澄清。它确定了SUM()的内容。当然,你可以改变这个条件。

GROUP BY s.squad_id, s.ladder_id, s.team_id, x.experience_id

感谢。