jQuery Datatable:使用Left Join和Where子句对记录进行计数

时间:2018-10-23 03:31:16

标签: mysql

我有两个表,“团队”和“票”。现在,它会显示所有记录,还显示团队成员已分派的罚单。

我现在需要计算每个团队的门票数量。

http://sqlfiddle.com/#!9/609d4f/18

这是我的尝试:

SELECT
    team.techid,
    team.name,
    tickets.techid,
    tickets.customer,
    tickets.callstatus,
    tickets.serialnumber
FROM team
LEFT JOIN tickets
    ON tickets.techid = team.techid AND (tickets.callstatus = 'Dispatch') AND 
(COUNT(tickets.customer) WHERE tickets.techid = team.techid )

更新

工作示例,但仅缺少计数列:

http://sqlfiddle.com/#!9/609d4f/19

Update2

蒂姆,谢谢您的帮助,但您的示例不起作用。

该表应如下所示,当然要减去缺失的列:

|---------------------|------------------|
|      Tech ID        |     Count        |
|---------------------|------------------|
|         Tech1       |      1           |
|---------------------|------------------|
|         Tech2       |      1           |
|---------------------|------------------|
|         Tech3       |      0           |
|---------------------|------------------|

http://sqlfiddle.com/#!9/bfcdf5/1

如下所示,tech1和tech2在ato_openservicecalls中都有记录,其中SC_CallStatus为Dispatch

insert into `serviceteam` VALUES (1, 'tech1', 'name1', 'manager1', 'dispatcher1', 'cellphone1');
insert into `serviceteam` VALUES (2, 'tech2', 'name2', 'manager2', 'dispatcher2', 'cellphone2');
insert into `serviceteam` VALUES (3, 'tech3', 'name3', 'manager3', 'dispatcher3', 'cellphone3');


insert into `ato_openservicecalls` VALUES (1, 'tech1', 'Dispatch', 'customer1', 'age1', 'timestamp1', 'serial1', 'comment1');
insert into `ato_openservicecalls` VALUES (2, 'tech2', 'Dispatch', 'customer2', 'age2', 'timestamp2', 'serial2', 'comment2');
insert into `ato_openservicecalls` VALUES (3, 'tech3', 'callstatus3', 'customer3', 'age3', 'timestamp3', 'serial3', 'comment3');



SELECT
t1.techid,
t1.techname,
t1.manager,
t1.dispatcher,
t1.cellphone,
t2.SC_SCTechID,
t2.BCARNA,
t2.SC_CallStatus,
t2.Serial_ID,
t2.Age,
t2.SC_CallTimestamp,
t2.SC_CallComment,
COALESCE(t3.num_tickets, 0) AS num_tickets
FROM serviceteam t1
LEFT JOIN ato_openservicecalls t2
    ON t1.techid = t2.SC_SCTechID AND t2.SC_CallStatus = 'Dispatch'
    LEFT JOIN
(
    SELECT t1.techid, COUNT(*) AS num_tickets
    FROM serviceteam t1
    INNER JOIN ato_openservicecalls t2
        ON t1.techid = t2.SC_SCTechID
    WHERE t2.SC_CallStatus = 'Dispatch'
) t3
    ON t1.techid = t3.techid;

1 个答案:

答案 0 :(得分:1)

在早于8+的MySQL版本中,我们可以使用子查询找到计数,然后加入该子查询:

SELECT
    t1.techid,
    t1.name,
    t2.techid,
    t2.customer,
    t2.callstatus,
    t2.serialnumber,
    COALESCE(t3.num_tickets, 0) AS num_tickets
FROM team t1
LEFT JOIN tickets t2
    ON t1.techid = t2.techid AND t2.callstatus = 'Dispatch'
LEFT JOIN
(
    SELECT t1.techid, COUNT(*) AS num_tickets
    FROM team t1
    INNER JOIN tickets t2
        ON t1.techid = t2.techid
    WHERE t2.callstatus = 'Dispatch'
) t3
    ON t1.techid = t3.techid;

在MySQL 8+或更高版本中,我们可以利用分析功能:

SELECT
    t1.techid,
    t1.name,
    t2.techid,
    t2.customer,
    t2.callstatus,
    t2.serialnumber,
    COUNT(t2.customer) OVER (PARTITION BY t1.techid) num_tickets
FROM team t1
LEFT JOIN tickets t2
    ON t1.techid = t2.techid AND t2.callstatus = 'Dispatch';

编辑:

您彻底改变了问题,使我的第一个被接受的答案无效。这是新的查询:

SELECT
    t1.techid,
    COUNT(t2.customer) AS num_tickets
FROM team t1
LEFT JOIN tickets t2
    ON t1.techid = t2.techid AND t2.callstatus = 'Dispatch'
GROUP BY
    t1.techid;