MySQL将几个查询合并为一个

时间:2015-11-29 09:13:10

标签: php mysql merge

我有一个名为“排名”的表,我需要来自联盟数据库的各种信息。 我创建了单个查询,但我想帮助合并到一两个查询中。

我的活动表:

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

1 个答案:

答案 0 :(得分:1)

这两个查询可以合并:

SELECT COUNT(*) AS TotalPlaces, SUM(amount) AS TotalEarnings 
FROM standings 
WHERE player_id = 31

最后使用CASE和小hack(转换COUNT - > SUM(CASE WHEN ...那么1 ELSE 0 END)):

SELECT COUNT(*) AS TotalPlaces, SUM(amount) AS TotalEarnings,
  SUM(CASE WHEN standing = 1 THEN 1 ELSE 0 END) AS FirstPlaceStandings,
  SUM(CASE WHEN standing = 1 THEN amount ELSE 0 END) AS FirstPlaceEarnings,
  SUM(CASE WHEN standing = 2 THEN 1 ELSE 0 END) AS SecondPlaceStandings,
  SUM(CASE WHEN standing = 2 THEN amount ELSE 0 END) AS SecondPlaceEarnings,
  SUM(CASE WHEN standing = 3 THEN 1 ELSE 0 END) AS ThirdPlaceStandings,
  SUM(CASE WHEN standing = 3 THEN amount ELSE 0 END) AS ThirdPlaceEarnings
FROM standings 
WHERE player_id = 31