简体   繁体   English

MySQL将多个查询合并为一个

[英]MySQL Merge Several Queries Into One

I have one table called "standings" that I need various pieces of information from for my league database. 我有一张桌子叫做“ stands”,我需要从我的联赛数据库中获得各种信息。 I have created the individual queries but I would like help merging into one or two queries. 我已经创建了单个查询,但是希望合并到一个或两个查询中。

My Events Table: 我的活动表:

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    

1st Place Queries 第一名查询

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 

2nd Place Queries 第二名查询

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 

3rd Place Queries 第三名查询

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

Totals Queries 总计查询

SELECT COUNT(*) AS TotalPlaces FROM standings WHERE player_id = 31

SELECT SUM(amount) AS TotalEarnings FROM standings WHERE player_id = 31

And here is my sample data: 这是我的示例数据:

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

This two queries can be merged: 这两个查询可以合并:

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

and finally use CASE and little hack (convert COUNT -> SUM(CASE WHEN ... THEN 1 ELSE 0 END)): 最后使用CASE和一点hack (转换COUNT-> SUM(CASE WHEN ... THEN 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM