使用JOIN和COUNT查找总计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 
,

国家/地区的计数:

switch (lang) {
    case 'german':
        translate(number-1,german);
        alert(translatedNumber);
        break;
    case 'french':
        translate(number-1,french);
        alert(translatedNumber);
        break;
    default:
        alert('Only French or German is allowed');
        break;
}

员工人数:

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; 中,我传递了列名。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...