如何在mysql中使用group by和count

时间:2018-03-21 12:46:49

标签: mysql sql

这里我有3张桌子使用我必须计算

  

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

id       allocationId    escortId

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

cab_allocation

 allocationId    allocationType

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

使用上表我必须得到计数,我试过但是没有发生我预期的结果。

我尝试过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

我是这样的

tripId

01LEC
ruaoR
osseC
aIz2o
S60zF

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

但此查询无效。我收到的结果如下

 2
2
1
1
1
  

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

3 个答案:

答案 0 :(得分:1)

有了这个,你应该得到tripid和金额:

console.log

答案 1 :(得分:1)

你可以试试这个

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

由于GROUP BY所有 allocationId 都有单独的计数。

答案 2 :(得分:0)

您可以使用:

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

之前您使用了COUNT(*)并使用了GROUP BY,因此您获得的行数来自各个组。

<强>更新-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