加入呼叫表,以按始发地和目的地分组

时间:2019-03-03 17:04:38

标签: php mysql sql database join

Call system

这是一个呼叫系统。

表结构和数据示例:

tbl_countries

CREATE TABLE tbl_countries
(
    country_id int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
    country varchar(100) NOT NULL
);
INSERT INTO tbl_countries (country) VALUES ('Austria');
INSERT INTO tbl_countries (country) VALUES ('Brazil');
INSERT INTO tbl_countries (country) VALUES ('Denmark');
INSERT INTO tbl_countries (country) VALUES ('France');
INSERT INTO tbl_countries (country) VALUES ('Lebanon');

tbl_destination_clients

CREATE TABLE tbl_destination_clients
(
    des_client_id int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
    des_client varchar(100) NOT NULL,
    country_id int(11) NOT NULL,
    CONSTRAINT tbl_destination_clients_ibfk_1 FOREIGN KEY (country_id) REFERENCES tbl_countries (country_id)
);
CREATE INDEX country_id ON tbl_destination_clients (country_id);
INSERT INTO tbl_destination_clients (des_client, country_id) VALUES ('961123456', 5);
INSERT INTO tbl_destination_clients (des_client, country_id) VALUES ('55123456', 2);
INSERT INTO tbl_destination_clients (des_client, country_id) VALUES ('45123456', 3);

tbl_origination_clients

CREATE TABLE tbl_origination_clients
(
    orig_client_id int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
    orig_client varchar(100) NOT NULL,
    country_id int(11) NOT NULL,
    CONSTRAINT tbl_origination_clients_ibfk_1 FOREIGN KEY (country_id) REFERENCES tbl_countries (country_id)
);
CREATE INDEX country_id ON tbl_origination_clients (country_id);
INSERT INTO tbl_origination_clients (orig_client, country_id) VALUES ('33123456', 4);
INSERT INTO tbl_origination_clients (orig_client, country_id) VALUES ('43123456', 1);

tbl_calls

CREATE TABLE tbl_calls
(
    call_id int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
    orig_client_id int(11) NOT NULL,
    des_client_id int(11) NOT NULL,
    call_duration int(11) NOT NULL,
    CONSTRAINT tbl_calls_ibfk_1 FOREIGN KEY (orig_client_id) REFERENCES tbl_origination_clients (orig_client_id),
    CONSTRAINT tbl_calls_ibfk_2 FOREIGN KEY (des_client_id) REFERENCES tbl_destination_clients (des_client_id)
);
CREATE INDEX des_client_id ON tbl_calls (des_client_id);
CREATE INDEX orig_client_id ON tbl_calls (orig_client_id);
INSERT INTO tbl_calls (orig_client_id, des_client_id, call_duration) VALUES (1, 3, 5);
INSERT INTO tbl_calls (orig_client_id, des_client_id, call_duration) VALUES (1, 3, 6);
INSERT INTO tbl_calls (orig_client_id, des_client_id, call_duration) VALUES (2, 4, 9);
INSERT INTO tbl_calls (orig_client_id, des_client_id, call_duration) VALUES (1, 1, 10);

尝试次数:

我尝试在同一表tbl_calls之间使用JOIN,但是它不起作用;(

但是此示例数据的结果必须为:

Origination  Destination  SUM(call_duration)
------------+------------+----------------
France       Brazil       11 -> (5+6)
Austria      Denmark      9
France       Lebanon      10

谢谢@forpas

使用已修改的联接列进行查询

select
  co.country Origination,
  cd.country Destination,
  sum(c.call_duration) TotalDuration
from tbl_calls c 
inner join tbl_destination_clients d on d.des_client_id = c.des_client_id
inner join tbl_origination_clients o on o.orig_client_id = c.orig_client_id
inner join tbl_countries cd on cd.country_id = d.country_id
inner join tbl_countries co on co.country_id = o.country_id
group by   
  co.country,
  cd.country;

1 个答案:

答案 0 :(得分:1)

在多个联接之后进行分组(tbl_countries必须联接两次才能到达目的地和始发地)
并加总到call_duration

select
  co.country Origination,
  cd.country Destination,
  sum(c.call_duration) TotalDuration
from tbl_calls c 
inner join tbl_destination_clients d on d.des_client_id = c.des_client_id
inner join tbl_origination_clients o on o.orig_client_id = c.des_client_id
inner join tbl_countries cd on cd.country_id = d.country_id
inner join tbl_countries co on co.country_id = o.country_id
group by   
  co.country,
  cd.country;