Add an ordinal column to a MySQL query

Source: Internet
Author: User
Tags mysql query

Select *, (@number: = @number + 1) as number  from        (SelectU.mobile,u. ' Name ',ROUND((Select  sum(R.realamount) fromT_recharge RwhereR.userid=U.id andR.status='1'),2) Rechargesum,ROUND((Select  sum(W.realamount) fromT_withdrawal WwhereW.userid=U.id andW.status='1'),2) Withdrawalsum,ROUND(sum(F.ownamount),2) Ownamountsum,ROUND(sum(F.platformfunds),2) Platformfundssum,ROUND((Select sum(Fd.amount) fromT_fund_detail FDwhereFd.userid=U.id andFd.type='3'),2) Feesum,ROUND(sum(F.result),2) Cashresult, u.balance,u2. ' Name ' asfroms fromt_user u Left JoinT_financing F onF.userid=u.id Left JOINT_user U2 onU2.id=U.fromidGroup  byU.id)Temp,(select @number: =0) b

Used is (@number: = @number + 1) as number and (select @number: =0) b, above is an example of multiple tables

An example of a table is as follows:

Add an ordinal column to a MySQL query

Related Article

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.