Preface In fact, I have never encountered this kind of situation where the results are numbered during a query before. It was only when my colleague was planning to change jobs and asked about this situation during the interview that I thought of studying it. The following is an analysis of a single table query as an example: SQL: SELECT (@i:=@i+1) i,user_id,user_name FROM `dt_user_all_orders`, (SELECT @i:=0) as i WHERE user_name='qqqqqqqqqqq' LIMIT 0,10; result: If you need to group and then display the sequence number: SELECT drug_productor,@y:=@y+1 as num FROM( SELECT drug_productor FROM ts_drug a GROUP BY drug_productor) c,(SELECT @y:=0) d result: analyze: At the beginning, a variable i is defined, and each increment of it results in ➕1, Here we review the way MySQL defines user variables: select @ variable name There are two ways to assign values to user variables, one is to use the "=" sign directly, and the other is to use the ":=" sign. The difference is that when using the set command to assign values to user variables, both methods can be used; when using the select statement to assign values to user variables, only the ":=" method can be used, because in the select statement, the "=" sign is regarded as a comparison operator. Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM. You may also be interested in:
|
<<: js implements a simple shopping cart module
>>: How to monitor and delete timed out sessions in Tomcat
Table of contents Overview Environment Preparatio...
This article shares the specific code of Vue to i...
I recently started learning Linux. After reading ...
We all know the drag-and-drop feature of HTML5, w...
Preface Students who learn JavaScript know that A...
Preface I watched web.dev's 2020 three-day li...
Preface: The Linux host is relatively easy to han...
In fact, this problem has already popped up when I...
Table of contents Preface Enumerable properties I...
MySQL version: MySQL Community Edition (GPL) ----...
Table of contents 1. Switch between production en...
Table of contents 1. Dep 2. Understand obverser 3...
WeChat applet trajectory playback mainly uses pol...
An n-digit verification code consisting of number...
MySQL is an open source, small relational databas...