简体   繁体   English

SQL查询子句的顺序

[英]Order of SQL Query Clauses

I ran into a situation where I don't seem to get what SQL is doing. 我遇到了一种情况,我似乎无法了解SQL在做什么。 I have the following table and want to give out all the sorts of coffee which have the most amount of rating=5 with the amount itself. 我有下表,并想给出所有等级= 5数量最多的咖啡。

create table likes
(
     CName varchar(30),
     UName varchar(30),
     Rating int
);

insert into likes (CName, UName, Rating) 
values ('Java', 'Klaus', '5'),
       ('Super', 'Klaus', '5'),
       ('MP', 'Klaus', '3'),
       ('Java', 'Marc', '5'),
       ('Mp', 'Marc', '5'),
       ('Super', 'Marc', '2'),
       ('Java', 'Nine', '2'),
       ('Super', 'Nine', '0'),
       ('MP', 'Karo', '3'),
       ('Super', 'Fabian', '4');

However this solution doesn't work as intended 但是,此解决方案无法按预期工作

SELECT 
    favcof.CName, favcof.cnt
FROM 
   (SELECT l.CName, COUNT(CName) cnt
    FROM likes l
    WHERE l.rating = 5
    GROUP BY CName) favcof
WHERE 
    favcof.cnt = (SELECT MAX(favcof.cnt))

It executes as if there is no outer where-clause and gives out all sorts of coffees with their amount of rating = 5. 它执行起来就好像没有外部子句,并给出等级为5的各种咖啡。

The expression (select max(favcof.cnt)) doesn't do anything. 表达式(select max(favcof.cnt))不执行任何操作。 You can just drop the select and you will get favcof.cnt = favcof.cnt . 您只需删除select ,您将获得favcof.cnt = favcof.cnt

This is a little complicated, because favcof.cnt = max(favcof.cnt) would generate a syntax error because aggregation functions are not allowed in the where clause. 这有点复杂,因为favcof.cnt = max(favcof.cnt)会生成语法错误,因为where子句中不允许使用聚合函数。 So, the select subquery is actually an aggregation subquery with no from . 因此, select子查询实际上是一个没有from的聚合子查询。 Because there is only one value, it returns that value. 因为只有一个值,所以它将返回该值。

You want a correlated subquery. 您需要一个相关的子查询。 This would look like: 看起来像:

SELECT favcof.CName, favcof.cnt
FROM (SELECT l.UName, count(UName) as cnt
      FROM likes l
      WHERE l.rating=5
      GROUP BY UName
     ) favcof
WHERE favcof.cnt = (SELECT MAX(favcof2.cnt)
                    FROM (SELECT l2.UName, count(l2.UName) as cnt
                          FROM likes l2
                          WHERE l2.rating=5
                          GROUP BY l2.UName
                         ) favcof2
                   );

There are definitely other ways to write this query. 肯定还有其他方式可以编写此查询。 However, this should help you understand why your version does not do what you want it to do. 但是,这应该可以帮助您了解为什么您的版本不执行您想要的操作。

you can do like this 你可以这样

DECLARE @likes AS TABLE(CName NVARCHAR(50), UName NVARCHAR(50), Rating INT)

insert into @likes (CName, UName, Rating) values
    ('Java', 'Klaus', '5'),
    ('Super', 'Klaus', '5'),
    ('MP', 'Klaus', '3'),
    ('Java', 'Marc', '5'),
    ('Mp', 'Marc', '5'),
    ('Super', 'Marc', '2'),
    ('Java', 'Nine', '2'),
    ('Super', 'Nine', '0'),
    ('MP', 'Karo', '3'),
    ('Super', 'Fabian', '4');

SELECT  UName, COUNT(CName) Cnt FROM @Likes
WHERE Rating = (SELECT MAX(Rating) FROM @Likes)
GROUP BY UNAME

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM