MySQL limit processes multiple returned results

Source: Internet
Author: User

Today, another Mysql Data Query problem occurs in the project. Simply put, when you want to process a query (multiple records may be returned), you can use

Limit to solve some problems.

Eg:

Select a. *, (select B. ID from table_ B B where...) as T1 from table_a

If: (select B. ID from table_ B B where ......) multiple results are returned, an error occurs. (Can I splice the returned results into a string in T1? What about parsing rules at the front end? Have thought about this problem
. However, limit can also be used to meet this requirement .)


The solution is as follows:

Select TT .*
, (Select S. name from tbl_rel_route_has_destination RHD, tbl_obj_scene s where RHD. sceneid = S. ID and RHD. routeid = (select sp. routeid from tbl_obj_sale_plan SP where sp. id = TT. planid) limit 1) as DES1
, (Select S. name from tbl_rel_route_has_destination RHD, tbl_obj_scene s where RHD. sceneid = S. ID and RHD. routeid = (select sp. routeid from tbl_obj_sale_plan SP where sp. id = TT. planid) limit 1, 1) as des2
From tbl_obj_travel_team TT
Where TT. cityid = (select gov. cityid from tbl_obj_government Gov, tbl_obj_user u where u. governmentid = gov. ID and U. ID = 1)

If limit 2nd, that is, the data entries in the query result do not exist, null is returned, and the frontend determines the result)

Limit usage:

First parameter: Start offset (0 ).

Second parameter: Limit the number of records to be queried.

Note:

1. If there is only one parameter. Eg: limit 10 returns the first 10 results. Limit N is equivalent to limit 0, n

2. Two parameters, for example: limit 10, 10, return 11-20.

3. to retrieve all record rows from an offset to the end of the record set, you can specify the second parameter
-1. limit 10,-1

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.