MySQL uses pseudo-columns

Source: Internet
Author: User

When querying the database, we sometimes need to add a sequence to the queried data, 1,2,3,......N

For example, we sort the data according to a field of the table, we want to add a sequence to this, when the serial number is often not the self-increment primary key ID that we set up when we build the table, how to do????

Maybe we'll use variables to solve them, as follows

SET @rownum:=0; SELECT @rownum:=@rownum+1 as rownum,a.*from T_s_base_user A;

If you want to use a SQL statement in the database to achieve this, then what to do????

The Oracle database has a dedicated rownum to solve the pseudo-sequence problem, but MySQL has so far not built-in functions to solve this problem

This problem also bothered me for a while, to a variety of posts have not been able to answer, and finally found this solution by accident

SELECT @rownum:=@rownum+1 as RowNum, T_s_base_user. *  from (SELECT@rownum:=0) R, T_s_base_user

Put the initial value of the rownum in front of the From and the table that needs to be queried

Maybe someone will use the stored procedure to solve, that is also a method, I have used, but later can not use stored procedures, it is tragic

But the program will error "Parameter ' @rownum ' must be defined."

Then add "Allow User variables=true" after the configuration file database link string.

<!-- MySQL Connection string  -         <  key= "Mysqlconn"  value= "SERVER=192.168.1.238;DATABASE=GM; Uid=root; pwd=000000; Allow User variables=true; " />

MySQL uses pseudo-columns

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.