简体   繁体   English

如何在MySQL中使用分组和计数

[英]How to use group by and count in mysql

Here I am having 3 table using I have to take the count 在这里我有3张桌子,我必须要数

trip_details trip_details

id    allocationId       tripId

1         7637             aIz2o
2         7626             osseC
3         7536             01LEC
4         7536             78w2w
5         7640             S60zF
6         7548             ruaoR
7         7548             Qse6s

escort_allocation escort_allocation

id       allocationId    escortId

3          7637            1
4          7626            1
5          7627            1
6          7536            1
7          7640            1
7          7548            1

cab_allocation cab_allocation

 allocationId    allocationType

 7637             Daily Trip
 7626             Daily Trip
 7627             Daily Trip
 7536              Adhoc Trip
 7640               Adhoc Trip
 7548               Daily Trip

Using above table I have to get the count, I tried but it is not happening my expected results. 我尝试使用上表来获取计数,但是尝试并没有实现预期的结果。

I tried sql query 我试过SQL查询

    SELECT a.`tripId` 
FROM  `trip_details` a
INNER JOIN escort_allocation b ON a.`allocationId` = b.`allocationId` 
GROUP BY a.`allocationId` 
LIMIT 0 , 30

I am getting like this 我越来越像这样

tripId

01LEC
ruaoR
osseC
aIz2o
S60zF

total 6 tripId i got ,so now I want to take the count so I am using this query total 6 tripId i got ,所以现在我想算一下,所以我正在使用此查询

    SELECT COUNT(*)
FROM  `trip_details` a
INNER JOIN escort_allocation b ON a.`allocationId` = b.`allocationId` 
GROUP BY a.`allocationId` 
LIMIT 0 , 30

but this query is not working. 但是此查询无法正常工作。 I am getting results like below 我得到如下结果

 2
2
1
1
1

MY MYSQL TABLES AND VALUES LOOK LIKE THIS 我的MYSQL表和值类似

    CREATE TABLE `trip_details` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `allocationId` int(11) NOT NULL,
 `tripId` varchar(100) NOT NULL,
 PRIMARY KEY (`id`),
 KEY `tripId` (`tripId`),
 KEY `allocationId` (`allocationId`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 ;

INSERT INTO trip_details
    (id, allocationId, tripId)
VALUES
    (1, 7637, '00SwM'),
    (2, 7626, '00SwM'),
    (3, 7536, '00SwM'),
    (4, 7536, '01hEU'),
    (5, 7640, '01hEU'),
    (6, 7548, 'IRZMS'),
    (7, 7548, 'IRZMS');




CREATE TABLE `escort_allocation` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `allocationId` int(11) NOT NULL,
 `escortId` varchar(100) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 ;

INSERT INTO escort_allocation
    (id, allocationId, escortId)
VALUES
    (1, 7637, 'ssda'),
    (2, 7626, 'adad'),
    (3, 7627, 'sfsaf'),
    (4, 7536, 'ssaf'),
    (5, 7640, 'asf'),
    (6, 7548, 'a3r');



CREATE TABLE `cab_allocation` (
 `allocationId` int(11) NOT NULL AUTO_INCREMENT,
 `allocationType` enum('Daily Trip','Adhoc Trip') NOT NULL,
 PRIMARY KEY (`allocationId`)
) ENGINE=InnoDB AUTO_INCREMENT=7695 DEFAULT CHARSET=latin1;

INSERT INTO cab_allocation
    (allocationId, allocationType)
VALUES
    (7637, 'Daily Trip'),
    (7626, 'Daily Trip'),
    (7627, 'Daily Trip'),
    (7536, 'Adhoc Trip'),
    (7640, 'Adhoc Trip'),
    (7548, 'Daily Trip');

With this, you should get the tripid and the amount: 这样,您应该获得三叉戟和数量:

SELECT COUNT(a.tripId) as total, a.tripId as tripId 
FROM trip_details a INNER JOIN escort_allocation b 
ON a.allocationId = b.allocationId 
GROUP BY a.allocationId LIMIT 0 , 30

You can try this 你可以试试这个

 SELECT COUNT(DISTINCT (a.`tripId`))
 FROM  `trip_details` a
 INNER JOIN escort_allocation b ON a.`allocationId`=b.`allocationId`
 LIMIT 0 , 30

because of GROUP BY there is separate count for all allocationId . 由于GROUP BY缘故,所有分配 GROUP BY都有单独的计数。

You may use: 您可以使用:

SELECT COUNT(DISTINCT a.allocationId)
FROM
trip_details a
INNER JOIN
escort_allocation b
ON a.allocationId = b.allocationId

Previously you used COUNT(*) and also used GROUP BY so the counts of rows you were getting are from individual groups. 以前,您使用了COUNT(*)并且还使用了GROUP BY因此您获得的行数来自各个组。

Update-2: 更新2:

SELECT 
(
    SELECT COUNT(*) FROM trip_details
) AS Total_Trip_Count,
COUNT(T.tripId) as Escort_Count,
(
    SELECT COUNT(*) FROM 
    (
        SELECT a.allocationId
        FROM escort_allocation a 
        INNER JOIN cab_allocation c ON a.allocationId = c.allocationId 
        WHERE c.allocationType = 'Adhoc Trip' 
        GROUP BY a.allocationId
    ) AS Ad
) AS Adhoc_Trip_Count
FROM 
( 
    SELECT a.tripId FROM 
    trip_details a 
    INNER JOIN 
    escort_allocation b 
    ON a.allocationId = b.allocationId 
    GROUP BY a.allocationId 
) AS T

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

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