以下db的SQL查询

时间:2018-04-20 18:33:19

标签: sql

salesperson:(sid, name, age, salary)
customer :(cid, cname, city)
order:(oid, order_date, cid, sid, amount)

“查找具有两个或更多订单的销售人员姓名”的SQL查询

3 个答案:

答案 0 :(得分:0)

SELECT s.name, count(o.oid) FROM salesperson s, order o
WHERE s.sid=o.sid
GROUP BY s.name
HAVING count(o.oid)>=2

我们在这里使用的是"Inner Join",其中只提取了对应的行(where s.sid=o.sid)。

答案 1 :(得分:0)

SELECT s.name, count(*) order_count
FROM salesperson AS s, order AS o
WHERE s.sid = o.sid
GROUP BY s.name 
HAVING COUNT(*) > 1

这将返回名称以及与每个销售人员相关的订单数量

答案 2 :(得分:0)

测试良好

SELECT sp.NAME 
FROM   salesperson AS sp 
       INNER JOIN order AS ord 
               ON salesperson.sid = ord.sid 
GROUP  BY sp.sid 
HAVING Count(ord.oid) >= 2