MySQL takes the first 5 stepwise analysis of each category

I visited the interview before, and searched for a long time, and found that the problem can be solved by adding the line number.
First, there is a table with three fields goods, name, and price. There is a requirement: you
need to query everyone to buy the most expensive 5 What are the products?
Analysis: (that is, take out the top 5 expensive products by name)
Table structure:
Insert picture description here
Insert statement:

insert into shopping (goods, name, price) VALUES ("笔","小明",20.00);
insert into shopping (goods, name, price) VALUES ("杯子","小明",100.00);
insert into shopping (goods, name, price) VALUES ("花","小红",10.00);
insert into shopping (goods, name, price) VALUES ("车子","小红",100000.00);
insert into shopping (goods, name, price) VALUES ("房子","小王",1000000.00);
insert into shopping (goods, name, price) VALUES ("狗","小王",1000.00);
insert into shopping (goods, name, price) VALUES ("1","小王",1.00);
insert into shopping (goods, name, price) VALUES ("2","小王",10.00);
insert into shopping (goods, name, price) VALUES ("3","小王",100.00);
insert into shopping (goods, name, price) VALUES ("4","小王",1000.00);
insert into shopping (goods, name, price) VALUES ("5","小王",10000.00);
insert into shopping (goods, name, price) VALUES ("6","小王",0.100);

Step-by-step analysis:
1. At the beginning, the first idea of ​​seeing this query requirement is to group by name, but group by name will only group the column of name, and price actually takes each group according to the order in the table. You can check the
results of the experiment against the table above :
Insert picture description here
So it’s not advisable to use group by here.
2. If you want to get this category, what should you do? You can sort by name first, and then descend by price, that’s it A table:
Insert picture description here
3. But we still need to take out the top 5 of this category, how can we take it?
In fact, if you add the grouping row number to each category, and then take the row with rownum<=5, you can actually get the first 5 out. That's it, add the row number first:
Insert picture description here
then take the first 5:
Insert picture description here
then query The result of each person buying the 5 most expensive products:
The complete query statement here is:

select goods,name,price
    from (
        select
            @gn:=case when @name=name then @gn+1 else 1 end rownum,
            price,
            goods,
            @name:=name as name
        from shopping a ,(select @gn:=0,@name:=(select name from shopping order by name,price desc limit 1)) b
        order by name,price desc) aa
    where rownum<=5;

Finally, explain the meaning of the query statement here:
first try:
select @rowNum:=0;
this sentence means assigning 0 to the variable rowNum
and then try:
select @rowNum:=@rowNum+1,a.* from shopping a ,(select @rowNum:=0) b;
here I understand that rowNum can be regarded as a global variable, the first time the value is assigned to 0, when the first line of the query When rowNum is assigned to 1, it is associated with the field of table a; when the second row is queried, rowNum is assigned to 2, which is associated with the field of table a, and so on,,,
experimental results:
Insert picture description here
finally try this query,

  select
            @gn:=case when @name=name then @gn+1 else 1 end rownum,
            price,
            goods,
            @name:=name as name
        from shopping a ,(select @gn:=0,@name:=(select name from shopping order by name,price desc limit 1)) b
        order by name,price desc

First, name is assigned to the top "Xiaoming" after sorting for the first time, and then when the first row of shopping is queried, the variable name of @name is now "Xiaoming", and the name field is also Xiaoming at this time, gn =1, note that the order of assignment of the @name variable here is after the judgment statement, that is, after the judgment, the value "Xiao Ming" of the field name in the first line is again assigned to @name, so @name in the second line The value is "Xiaoming" and the value of name is Xiaoming. At this time, gn+1=2, and when the third line is reached, the value of @name is "Xiaoming", and the value of the field name becomes Xiaowang. So gn goes to else at this time, and it becomes 1 again, and the grouping line number is realized *^^*

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36875803/article/details/111408225