search
HomeDatabaseMysql Tutorial让SELECT 查询结果额外增加自动递增序号

让SELECT 查询结果额外增加自动递增序号

Jun 07, 2016 pm 02:54 PM
selectIncreaseserial numberInquireresultautomaticIncrement

如果数据表本身并不内含自动地增编号的字段时,要怎么做才能够让SELECT查询结果,额外增加自动递增序号呢?我们提供下列五种方法供您参考: 自增长 USE 2 GO 3 4 /* 方法一*/ 5 6 SELECT序号= (SELECT COUNT(客户编号) FROM 客户 AS LiMing 7 WHERE LiMing.

如果数据表本身并不内含自动地增编号的字段时,要怎么做才能够让SELECT查询结果,额外增加自动递增序号呢?我们提供下列五种方法供您参考: 自增长
USE
 2 GO
 3 
 4 /* 方法一*/
 5 
 6 SELECT序号= (SELECT COUNT(客户编号) FROM 客户 AS LiMing
 7                      WHERE LiMing.客户编号<= Chang.客户编号),
 8         客户编号,公司名称
 9 FROM客户 AS Chang ORDER BY 1;
10 GO
11 
12 /* 方法二: 使用SQL Server 2005 独有的RANK() OVER () 语法*/
13 SELECT RANK() OVER (ORDER BY 客户编号 DESC) AS 序号,
14           客户编号,公司名称
15 FROM客户;
16 GO
17 
18 /* 方法三*/
19 SELECT序号= COUNT(*), LiMing.客户编号, LiMing.公司名称
20     FROM 客户 AS LiMing, 客户AS Chang
21     WHERE LiMing.客户编号>= Chang.客户编号
22     GROUP BY LiMing.客户编号, LiMing.公司名称
23     ORDER BY 序号;
24 GO
25 
26 /* 方法四
27 建立一个「自动编号」的字段,然后将数据新增至一个区域性暂存数据表,
28 然后由该区域性暂存数据表中,将数据选取出来,最后删除该区域性暂存数据表
29 */
30 SELECT序号= IDENTITY(INT,1,1),管道,程序语言,讲师,资历
31 INTO #LiMing
32 FROM问券调查一;
33 GO
34 SELECT * FROM #LiMing;
35 GO
36 DROP TABLE #LiMing;
37 GO
38 
39 /*
40 方法五
41 使用 SQL Server 2005 独有的ROW_NUMBER() OVER () 语法
42 搭配 CTE (一般数据表表达式,就是 WITH 那段语法)选取序号2 ~ 4 的数据
43 */
44 WITH排序后的图书 AS
45 (SELECT ROW_NUMBER() OVER (ORDER BY 客户编号 DESC) AS 序号,
46    客户编号,公司名称
47     FROM 客户)
48 SELECT * FROM 排序后的图书
49 WHERE序号 BETWEEN 2 AND 4;
50 GO
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.