【发布时间】:2015-11-29 09:13:10
【问题描述】:
我有一张名为“排名”的表,我需要从我的联赛数据库中获取各种信息。 我已经创建了单独的查询,但我希望帮助合并成一两个查询。
我的事件表:
id int(11)
event_id int(11)
player_id int(11)
tournament_id int(11)
location_id int(11)
standing varchar(10)
amount int(11)
created datetime
第一名查询
SELECT COUNT(*) AS FirstPlaceStandings FROM standings WHERE player_id = 31 AND standing = 1
SELECT SUM(amount) AS FirstPlaceEarnings FROM standings WHERE player_id = 31 AND standing = 1
第二名查询
SELECT COUNT(*) AS SecondPlaceStandings FROM standings WHERE player_id = 31 AND standing = 2
SELECT SUM(amount) AS SecondPlaceEarnings FROM standings WHERE player_id = 31 AND standing = 2
第三名查询
SELECT COUNT(*) AS ThirdPlaceStandings FROM standings WHERE player_id = 31 AND standing = 3
SELECT SUM(amount) AS ThirdPlaceEarnings FROM standings WHERE player_id = 31 AND standing = 3
查询总数
SELECT COUNT(*) AS TotalPlaces FROM standings WHERE player_id = 31
SELECT SUM(amount) AS TotalEarnings FROM standings WHERE player_id = 31
这是我的示例数据:
CREATE TABLE IF NOT EXISTS `standings` (
`id` int(11) NOT NULL,
`event_id` int(11) NOT NULL,
`player_id` int(11) NOT NULL,
`tournament_id` int(11) NOT NULL,
`location_id` int(11) NOT NULL,
`standing` varchar(10) NOT NULL,
`amount` int(11) NOT NULL,
`created` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=49 ;
--
-- Dumping data for table `standings`
--
INSERT INTO `standings` (`id`, `event_id`, `player_id`, `tournament_id`, `location_id`, `standing`, `amount`, `created`) VALUES
(7, 8, 32, 14, 12, '1', 101, '2015-11-22 12:52:07'),
(8, 8, 31, 14, 12, '2', 60, '2015-11-22 12:52:07'),
(9, 8, 27, 14, 12, '3', 30, '2015-11-22 12:52:07'),
(10, 9, 30, 18, 16, '1', 150, '2015-11-22 14:45:11'),
(11, 9, 32, 18, 16, '2', 95, '2015-11-22 14:45:11'),
(12, 9, 27, 18, 16, '3', 40, '2015-11-22 14:45:11'),
(13, 10, 26, 14, 12, '1', 155, '2015-11-22 14:46:22'),
(14, 10, 28, 14, 12, '2', 97, '2015-11-22 14:46:22'),
(15, 10, 32, 14, 12, '3', 45, '2015-11-22 14:46:22'),
(19, 12, 31, 18, 16, '1', 100, '2015-11-22 16:03:50'),
(20, 12, 27, 18, 16, '2', 60, '2015-11-22 16:03:50'),
(21, 12, 30, 18, 16, '3', 20, '2015-11-22 16:03:50'),
(22, 13, 27, 8, 2, '1', 108, '2015-11-22 16:20:13'),
(23, 13, 31, 8, 2, '2', 67, '2015-11-22 16:20:13'),
(24, 13, 29, 8, 2, '3', 27, '2015-11-22 16:20:13'),
(25, 14, 31, 14, 14, '1', 500, '2015-11-27 20:22:17'),
(26, 14, 26, 14, 12, '2', 250, '2015-11-27 20:22:17'),
(27, 14, 29, 14, 12, '3', 125, '2015-11-27 20:22:17'),
(28, 15, 27, 38, 2, '1', 376, '2015-11-27 20:23:44'),
(29, 15, 28, 38, 2, '2', 200, '2015-11-27 20:23:44'),
(30, 15, 30, 38, 2, '3', 160, '2015-11-27 20:23:44'),
(34, 17, 32, 19, 18, '1', 100, '2015-11-28 21:46:45'),
(35, 17, 27, 19, 18, '2', 50, '2015-11-28 21:46:45'),
(36, 17, 26, 19, 18, '3', 25, '2015-11-28 21:46:45'),
(37, 18, 27, 14, 12, '1', 200, '2015-11-28 21:48:57'),
(38, 18, 26, 14, 12, '2', 100, '2015-11-28 21:48:57'),
(39, 18, 31, 14, 12, '3', 50, '2015-11-28 21:48:57'),
(40, 19, 26, 23, 19, '1', 250, '2015-11-28 21:50:09'),
(41, 19, 34, 23, 19, '2', 125, '2015-11-28 21:50:09'),
(42, 19, 33, 23, 19, '3', 76, '2015-11-28 21:50:09'),
(43, 20, 26, 23, 19, '1', 250, '2015-11-28 21:50:32'),
(44, 20, 34, 23, 19, '2', 125, '2015-11-28 21:50:32'),
(45, 20, 33, 23, 19, '3', 75, '2015-11-28 21:50:32'),
(46, 21, 33, 18, 16, '1', 500, '2015-11-28 21:56:37'),
(47, 21, 35, 18, 16, '2', 250, '2015-11-28 21:56:37'),
(48, 21, 29, 18, 16, '3', 125, '2015-11-28 21:56:37');
【问题讨论】:
-
那么,当您的问题与 PHP 无关时,为什么还要标记 PHP?
-
@icecub 这是 PHP PDO。使用我的方法,我将不得不来回访问数据库 8 次,这可能会也可能不会,因为每次结果集都很小。使用我的方法还是合并查询更好?
-
左连接/右连接等?
-
首先,考虑标准化你的数据库,也许收益可以在一个单独的表中。
-
@levi Zoesch 我可以在一张桌子上做吗?