在以下查询中,我尝试从表中获取数据:company
,classes_by_company
和person
。我在名为company_id
的所有表中都有一个外键。在classes_by_company
表中,一个类可以有一个class_status
,可以是active
或inactive
。表person
及其对应的person_status
也是如此。我可以计算班级总数和总人数,在这里查找工作查询:SQFIDDLE。在下面的查询中,我尝试合并上一个查询,但现在还计算class_status=ACTIVE
和person_status=ACTIVE
的总数。我收到错误Not unique table/alias: 'c'
。如何计算活动班级和人员的总数和总数? SQLFIDDLE
SELECT a.id, a.company_id, a.status, c.class_status, p.person_status,
c.total_count AS classes_per_company,
p.total_count AS employees_per_company,
c.active_count AS active_classes,
p.active_count AS active_instructors
FROM company a
LEFT JOIN (SELECT company_id, COUNT(*) as total_count
FROM classes_by_company
GROUP BY company_id) c
ON a.company_id = c.company_id
LEFT JOIN (SELECT company_id, COUNT(*) as active_count
FROM classes_by_company
WHERE class_status = 'ACTIVE'
GROUP BY company_id) c
ON a.company_id = c.company_id
LEFT JOIN (SELECT company_id, COUNT(*) as total_count
FROM person
GROUP BY company_id) p
ON a.company_id = p.company_id
LEFT JOIN (SELECT company_id, COUNT(*) as active_count
FROM person
WHERE person_status = 'ACTIVE'
GROUP BY company_id) p
ON a.company_id = c.company_id
表格结构:
CREATE TABLE company
(
id int auto_increment primary key,
company_id int,
status varchar(20)
);
CREATE TABLE classes_by_company
(
id int auto_increment primary key,
company_id int,
class_name varchar(20),
class_status varchar(20)
);
CREATE TABLE person
(
id int auto_increment primary key,
employee_id int,
company_id int,
person_name varchar(20),
person_status varchar(20)
);
答案 0 :(得分:1)
非常小的变化......表别名(或子查询)不能相同
SELECT a.id, a.company_id, a.status, c.class_status, p.person_status,
c.total_count AS classes_per_company,
p.total_count AS employees_per_company,
ca.active_count AS active_classes,
pa.active_count AS active_instructors
FROM company a
LEFT JOIN (SELECT company_id, COUNT(*) as total_count
FROM classes_by_company
GROUP BY company_id) c
ON a.company_id = c.company_id
LEFT JOIN (SELECT company_id, COUNT(*) as active_count
FROM classes_by_company
WHERE class_status = 'ACTIVE'
GROUP BY company_id) ca
ON a.company_id = c.company_id
LEFT JOIN (SELECT company_id, COUNT(*) as total_count
FROM person
GROUP BY company_id) p
ON a.company_id = p.company_id
LEFT JOIN (SELECT company_id, COUNT(*) as active_count
FROM person
WHERE person_status = 'ACTIVE'
GROUP BY company_id) pa
ON a.company_id = c.company_id
答案 1 :(得分:1)
您的查询结构很好,您只需要为您加入的每个子查询提供不同的别名。
您也未能从class_status
和person_status
子查询返回c
和p
。
SELECT a.id, a.company_id, a.status, c.class_status, p.person_status,
c.total_count AS classes_per_company,
p.total_count AS employees_per_company,
ca.active_count AS active_classes,
pa.active_count AS active_instructors
FROM company a
LEFT JOIN (SELECT company_id, class_status, COUNT(*) as total_count
FROM classes_by_company
GROUP BY company_id) c
ON a.company_id = c.company_id
LEFT JOIN (SELECT company_id, COUNT(*) as active_count
FROM classes_by_company
WHERE class_status = 'ACTIVE'
GROUP BY company_id) ca
ON a.company_id = ca.company_id
LEFT JOIN (SELECT company_id, person_status, COUNT(*) as total_count
FROM person
GROUP BY company_id) p
ON a.company_id = p.company_id
LEFT JOIN (SELECT company_id, COUNT(*) as active_count
FROM person
WHERE person_status = 'ACTIVE'
GROUP BY company_id) pa
ON a.company_id = pa.company_id
我将别名更改为ca
表示活动类,而pa表示活动人员。
您还可以通过计算同一查询中的活动行数来减少连接数:
SELECT a.id, a.company_id, a.status, c.class_status, p.person_status,
c.total_count AS classes_per_company,
p.total_count AS employees_per_company,
c.active_count AS active_classes,
p.active_count AS active_instructors
FROM company a
LEFT JOIN (SELECT company_id, class_status, COUNT(*) as total_count, SUM(class_status = 'ACTIVE') as active_count
FROM classes_by_company
GROUP BY company_id) c
ON a.company_id = c.company_id
LEFT JOIN (SELECT company_id, person_status, COUNT(*) as total_count, SUM(person_status = 'ACTIVE') as active_count
FROM person
GROUP BY company_id) p
ON a.company_id = p.company_id