使用 JOIN 和 COUNT 求总数 SQL
Using JOIN and COUNT to find totals SQL
我是 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)
);
非常感谢!
每个国家有多少客户
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
国家/地区计数:
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()
中,我已经传递了列名。
我是 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)
);
非常感谢!
每个国家有多少客户
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
国家/地区计数:
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()
中,我已经传递了列名。