当前位置:网站首页>Display line number in MySQL query result
Display line number in MySQL query result
2022-06-13 03:29:00 【TRX1024】
How to get a line number ? The key : Custom variable @rowNum:=0;
Suppose there is such data
mysql> select id,name from students;
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
| 2 | lisi |
| 3 | wangwu |
| 4 | trx |
| 5 | pjf |
| 6 | wzm |
+----+----------+
Look again
mysql> SELECT @rowNum:=0;
+------------+
| @rowNum:=0 |
+------------+
| 0 |
+------------+
mysql> SELECT @rowNum:=1;
+------------+
| @rowNum:=1 |
+------------+
| 1 |
+------------+
SELECT @rowNum:=0; Means to declare a name rowNum And assign it to 0, The variable name is customized , stay sql We can calculate this variable in , Such as :
mysql> select (@rowNum:[email protected]+1) as num from (SELECT @rowNum:=0) a;
+------+
| num |
+------+
| 1 |
+------+
mysql> select (@rowNum:[email protected]+5) as num from (SELECT @rowNum:=0) a;
+------+
| num |
+------+
| 5 |
+------+
here (SELECT @rowNum:=0) a Equivalent to a temporary table , If you compare it with students Join the tables and you will get :
mysql> select b.*,id,name from students a,(SELECT @rowNum:=0) b;
+------------+----+----------+
| @rowNum:=0 | id | name |
+------------+----+----------+
| 0 | 1 | zhangsan |
| 0 | 2 | lisi |
| 0 | 3 | wangwu |
| 0 | 4 | trx |
| 0 | 5 | pjf |
| 0 | 6 | wzm |
+------------+----+----------+
You can see ,b The table has only one piece of data , And a After the tables are connected, there are a Tabular 6 Data , If we set the custom variable @rowNum: Conduct +1 operation , What effect will it have ?
mysql> select (@rowNum:[email protected]+1) as num,id,name from students a,(SELECT @rowNum:=0) b;
+------+----+----------+
| num | id | name |
+------+----+----------+
| 1 | 1 | zhangsan |
| 2 | 2 | lisi |
| 3 | 3 | wangwu |
| 4 | 4 | trx |
| 5 | 5 | pjf |
| 6 | 6 | wzm |
+------+----+----------+
6 rows in set (0.00 sec)
This achieves the purpose of query results with line numbers , Let's deduce the process of joining two tables :
- Start with custom variables @rowNum:=0,a Table 1 data (1,'zhangsan') And custom variables join, But we took @rowNum:[email protected]+1, namely 0+1, Then you will finally get (1,1,'zhangsan')
- At this point, the custom variable @rowNum:=1,a Table 1 data (2,'lisi') And custom variables join, namely 1+1, Then you finally get (2,2,'lisi')
- ....( And so on )
thus , We can get sql Query the result with line number .
边栏推荐
- Data from the first to seventh census (to counties)
- (9) Explain broadcasting mechanism in detail
- Differences between XAML and XML
- This article takes you to learn DDD, basic introduction
- Panel data set of rural cities and towns: per capita consumption and expenditure of prefecture level cities 2012-2019 & rural data of provinces 2013-2019
- Neil eifrem, CEO of neo4j, interprets the chart data platform and leads the development of database in the next decade
- Scala method and function notes
- How to write product requirements documents
- Alibaba cloud OSS access notes
- MySQL group commit
猜你喜欢
[azure data platform] ETL tool (7) - detailed explanation of ADF copy data
YoloV5-Face+TensorRT:基于WIN10+TensorRT8.2+VS2019得部署
C # simple understanding - method overloading and rewriting
[azure data platform] ETL tool (4) - azure data factory debug pipeline
2000-2019 enterprise registration data of provinces, cities and counties in China (including longitude and latitude, number of registrations and other multi indicator information)
Feign based remote service invocation
[JVM Series 5] JVM tuning instance
在JDBC连接数据库时报错:Connection to 139.9.130.37:15400 refused.
Get to know druid IO real time OLAP data analysis storage system
Panel data set of rural cities and towns: per capita consumption and expenditure of prefecture level cities 2012-2019 & rural data of provinces 2013-2019
随机推荐
2000-2019 enterprise registration data of provinces, cities and counties in China (including longitude and latitude, number of registrations and other multi indicator information)
On the career crisis of programmers at the age of 35
MySQL learning summary Xi: detailed explanation of the use of stored procedures and stored functions
PHP uses the header function to download files
在JDBC连接数据库时报错:Connection to 139.9.130.37:15400 refused.
P1048 [noip2005 popularization group] Drug collection
Common command records of redis client
IP address and classification
Alibaba cloud OSS access notes
[synchronization function] version 2.0.16-19 has the update of synchronization function repair, but the problem has not been solved
(9) Explain broadcasting mechanism in detail
Graph data modeling tool
2000-2019 enterprise registration data of all provinces, cities and counties in China (including longitude and latitude, registration number and other multi indicator information)
Neo4j auradb free, the world's leading map database
The most complete ongdb and neo4j resource portal in history
look on? What is the case between neo4j and ongdb?
Scala method and function notes
Dish recommendation system based on graph database
Part II. S3. intuitionistic fuzzy multi-attribute decision-making method when attribute weight is intuitionistic fuzzy number
A data modeling optimization solution for graph data super nodes