MySQL uses user variables to update group sorting

Source: Internet
Author: User

The first requirement is sorted according to the A field, and the sort results are updated to the B field

After a simple search, you get answers quickly.

Http://dev.mysql.com/doc/refman/5.7/en/update.html

SET @a = 0 ; UPDATE SET = SELECT @a :=@a+1 )  ORDERbyASC 

The second requirement is to sort the B field according to the A field, sort the results to the C field

SET @c = 0;SET @A = 0;UPDATETableNameSETC= (SELECT  Case  when @A =A Then @c:= @c + 1ELSE     (@c:= @c + 1  and @A:=A)END)ORDER  byA, b

The result is correct, but I don't quite understand why ...

To be continue

MySQL uses user variables to update group sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.