有人可以深入说明查询吗?

发布于 2025-01-26 19:02:13 字数 2194 浏览 3 评论 0原文

问题:编写一个SQL查询,以从拥有多个客户的给定表中找到销售人员。返回salesman_id名称

示例表:<代码>客户

CityCitySklesman_id3002Nick
RimandoNew10050013007
BradDavisYork200513001
3005ZusiCalifornia California50023002
customer_idGREEN2002cust_nameLONED
JULIANGREENNewYorkGraham
JULIANCalifornia CameronBerlin1005003
3003Jozy AltidorMoscow2005007
3001Brad GuzanLondon5005

样本表:推销员

salesman_idnameCommissionPARIS PARIS PARIS PARIS PARIS PARIS PARIS PARIS
5001James HoogNew York New York New York0.15
5002Nail Knite nail KniteParis0.13
PARIS PARIS PARIS PARIS PARIS PARIS PARIS PARIS 0.11 5005PIT ALEX伦敦0.11
5006MC MC LYONPARISPIT 0.14
5007Paul AdamRome0.13
5003LAUSON HENSAN JOSE0.12

解决方案,

SELECT salesman_id,name 
FROM salesman a 
WHERE 1 < (SELECT COUNT(*) 
           FROM customer 
           WHERE salesman_id = a.salesman_id);

因此基本上无法理解该子查询我们如何在不使用加入的情况下比较两个表列,其次是该条件在某种程度上是如何按组来工作的,并提供计数。因此,如果有人可以用简单的语言深入了解这个子查询,那么它将确实很有帮助。

这个问题和解决方案是正确的,因为正在练习W3Resource,您可以在练习11中的SQL子查询练习中找到此问题。

Question: write a SQL query to find the salespeople from the given tables who had more than one customer. Return salesman_id and name.

Sample table: customer

customer_idcust_namecitygradesalesman_id
3002Nick RimandoNew York1005001
3007Brad DavisNew York2005001
3005Graham ZusiCalifornia2005002
3008Julian GreenLondon3005002
3004Fabian JohnsonParis3005006
3009Geoff CameronBerlin1005003
3003Jozy AltidorMoscow2005007
3001Brad GuzanLondon5005

Sample table: salesman

salesman_idnamecitycommission
5001James HoogNew York0.15
5002Nail KniteParis0.13
5005Pit AlexLondon0.11
5006Mc LyonParis0.14
5007Paul AdamRome0.13
5003Lauson HenSan Jose0.12

Solution

SELECT salesman_id,name 
FROM salesman a 
WHERE 1 < (SELECT COUNT(*) 
           FROM customer 
           WHERE salesman_id = a.salesman_id);

So basically am unable to understand the subquery how can we compare two table column without using the joins and secondly how this where condition is working as a group by as well and provide the count. So if someone can help me understand this subquery in depth with easy language then it would be really helpful.

This question and solution is correct because am practicing on W3resource and you can find this question under the topic of SQL Subquery Exercises in the exercise 11.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥琐帝 2025-02-02 19:02:13

这是a 相关子提法。实际上,外部表的每个匹配行运行一次,使用联接条件作为滤波器标准,即它将计算所有行的所有行,然后从外部表中进行ID =第一个ID,然后ID = second ID来自外部表格,因此在。然后,每个ID的结果计数将传递到滤波外表行的哪个子句。

您也可以写一个等效的查询:

SELECT s.salesman_id,s.name 
FROM salesman a
INNER JOIN customer b
ON a.salesman_id = b.salesman_id
GROUP BY a.salesman_id
HAVING count(b.customer_id) >  1

This is an example of a correlated subquery. In effect, the subquery is run once for each matching row of the outer table, using the join condition as filter criteria i.e. it will count for all rows with id = first id from outer table, then id = second id from outer table and so on. The resulting count for each id is then passed to the WHERE clause for filtering the rows of the outer table.

You could also write an equivalent query like so:

SELECT s.salesman_id,s.name 
FROM salesman a
INNER JOIN customer b
ON a.salesman_id = b.salesman_id
GROUP BY a.salesman_id
HAVING count(b.customer_id) >  1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文