0

我是 SQL 的超级新手,一直在处理这个查询,但似乎无法让它工作。我需要总结类别(每个国家有多少客户以及每个员工有多少客户)。这是我试图在每个国家/地区获得总客户的方法:

COUNT(*) TotalCount,
country.id, country.country
FROM client
INNER JOIN country
ON client.country_id = country.id
GROUP BY country.id, country.country

这些是我的表:

CREATE TABLE employee(
    id INT AUTO_INCREMENT PRIMARY KEY,
    employee VARCHAR(40)
);

CREATE TABLE country (
    id INT AUTO_INCREMENT PRIMARY KEY,
    country VARCHAR(40)
);

CREATE TABLE client(
    id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
    name VARCHAR(40),
    email VARCHAR(40),
    sold BOOLEAN,
    date VARCHAR(40),
    email_type_id INT,
    employee_id INT,
    country_id INT,

    FOREIGN KEY(email_type_id) REFERENCES email_type(id),
    FOREIGN KEY(employee_id) REFERENCES employee(id),
    FOREIGN KEY(country_id) REFERENCES country(id)
);

太感谢了!

4

2 回答 2

1

每个国家有多少客户

select country , count(*) from country  inner join client on country.id=client.country_id 
group by country 

每个员工有多少客户

Select employee , count(*) from employee inner join client on client.employee_id =employee.id group by employee 
于 2020-08-27T07:02:30.183 回答
0

国家计数:

SELECT country , count(*) as countryCount from country INNER JOIN client on country.id=client.country_id GROUP BY country;

员工人数:

SELECT employee , count(*) as empCount from employee INNER JOIN client on client.employee_id =employee.id GROUP BY employee;

你也可以点击这个查询:::

SELECT country , count(country) as countryCount from country INNER JOIN client on client.country_id=country.id GROUP BY country;

SELECT employee , count(employee) as empCount from employee INNER JOIN client on employee.i=client.employee_id GROUP BY employee;

count()中,我已经传递了列名。

于 2020-08-27T08:35:54.870 回答