[英]How do I 'join' but keep things separate in tables in MySQL?
I have two tables.我有两张桌子。 Table Employees:
表员工:
EmployeeID (employees) LastName (employees) FirstName (employees)
1 Davolio Nancy
And Table Orders:和表订单:
OrderID (orders) CustomerID (orders) EmployeeID (orders)
10248 90 5
10278 45 1
10238 47 1
I redacted the full listing because it's hundreds of rows.我编辑了完整列表,因为它有数百行。
In the table Employees, the EmployeeID can uniquely identify an employee, meaning it will not repeat in the Employee table.在Employees表中,EmployeeID可以唯一标识一个员工,在Employee表中不会重复。 However in the Table 'Order' The employeeID can repeat several times because an employee can sell help with many orders.
然而,在表 'Order' 中,employeeID 可以重复多次,因为一个员工可以出售许多订单的帮助。
Anyway, I can see here that in the Orders table, an employeeID will repeat several times, which means I need to use COUNT(EmployeeID)>=2 somewhere in my MySQL code.无论如何,我可以在这里看到,在 Orders 表中,employeeID 会重复多次,这意味着我需要在 MySQL 代码的某处使用 COUNT(EmployeeID)>=2。
This is what I'd like:这就是我想要的:
EmployeeID Number of Orders
1 2
As you can see, the EmployeeID shows up twice in the "orders" table.如您所见,EmployeeID 在“订单”表中出现了两次。 So he sold 2 items, and it links to his 1 Employee ID.
所以他卖了 2 件商品,它链接到他的 1 个员工 ID。
So this is what I tried:所以这就是我尝试过的:
SELECT EmployeeID, COUNT(EmployeeID) FROM
employees A inner join
orders B
ON (A.EmployeeID=B.EmployeeID)
WHERE COUNT(B.EmployeeID >=2)
This is the output:这是输出:
Error: Column 'EmployeeID' in field list is ambiguous — ERROR CODE 1052错误:字段列表中的“EmployeeID”列不明确 — 错误代码 1052
I'm not sure how I would get this result in this scenario.我不确定在这种情况下如何得到这个结果。
You want to build groups of orders that belong to the same employee, and then filter on the count of rows per group.您希望构建属于同一员工的订单组,然后根据每个组的行数进行筛选。 For this, you can use
group by
and having
:为此,您可以使用
group by
并having
:
select employeeid, count(*) cnt_orders
from employees e
inner join orders o using(employeeid)
group by employeeid
having count(*) >= 2
Note that a join is not necessary here.请注意,此处不需要连接。 You can get the result you want directly from the table of orders:
你可以直接从订单表中得到你想要的结果:
select employeeid, count(*) cnt_orders
from orders
group by employeeid
having count(*) >= 2
There's no need to join with the employees
table, you can get the employee ID from orders
.不需要加入
employees
表,可以从orders
获取employee ID。 You would only need to join if you also need other information from the employee
table, such as their name.仅当您还需要
employee
表中的其他信息(例如他们的姓名)时才需要加入。
You need GROUP BY employeeID
to get a count for each employee.您需要
GROUP BY employeeID
来获取每个员工的计数。
>= 2
should not be inside the COUNT()
function, you want to compare the result. >= 2
不应该在COUNT()
函数内,您要比较结果。
You need to use HAVING
rather than WHERE
.您需要使用
HAVING
而不是WHERE
。 WHERE
is used to select the rows to process before aggregating. WHERE
用于在聚合前选择要处理的行。
You should use COUNT(*)
rather than COUNT(columnName)
unless you need to exclude null values of the column from the count.您应该使用
COUNT(*)
而不是COUNT(columnName)
除非您需要从计数中排除列的空值。
If you give an alias to the COUNT(*)
result, you can use that alias in the HAVING
clause rather than restating the function.如果为
COUNT(*)
结果提供别名,则可以在HAVING
子句中使用该别名,而不是重新声明函数。
SELECT EmployeeID, COUNT(*) AS number_of_orders
FROM orders B
GROUP BY EmployeeID
HAVING number_of_orders >= 2
The reason for your error about the ambiguous column is because both tables have EmployeeID
columns.关于模糊列的错误的原因是因为两个表都有
EmployeeID
列。 In the SELECT
list you need to specify A.EmployeeID
or B.EmployeeID
, just as you did in the ON
clause.在
SELECT
列表中,您需要指定A.EmployeeID
或B.EmployeeID
,就像您在ON
子句中所做的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.