Use limit in Mysql subquery

Source: Internet
Author: User

A problem occurred in the project over the past two days. An error is reported after mysql LIMIT is used.

This is the requirement. I have three tables: infor info table, mconfig material configuration table, and maaply material application form. The requirement is to read who applies for the materials in the application form.

First, write:
SELECT infor. name, infor. phone, infor. add,
Mconfig. mname, mapply. acount, from_unixtime (mapply. atime, '% Y-% m-% D') as 'atime'
FROM mapply right JOIN infor ON mapply. uid = infor. uid inner JOIN mconfig ON mapply. mid = mconfig. mid
WHERE mapply. aid
IN (
SELECT aid
FROM 'mapply' where state = $ state
Order by 'atime ', 'uid' DESC
LIMIT 0, 10
) Www.jbxue.com

An error is returned.

At that time, I did not pay attention to the reported error. I only saw the LIMIT error, so I modified the following code:
SELECT infor. name, infor. phone, infor. add,
Mconfig. mname, mapply. acount, from_unixtime (mapply. atime, '% Y-% m-% D') as 'atime'
FROM mapply right JOIN infor ON mapply. uid = infor. uid inner JOIN mconfig ON mapply. mid = mconfig. mid
WHERE mapply. aid
IN (
SELECT aid
FROM 'mapply' where state = $ state
Order by 'atime ', 'uid' DESC
)
<Pre name = "code" class = "SQL"> LIMIT 0, 10 </pre>

In this case, no error is reported. Mo Li thought it was OK, but after running it, the data was faulty.

Unlike reading the content of the application form, I found that the LIMIT location was wrong. So I sent the LIMIT to IN again. The result is as follows:

This version of MySQL doesn't yet support 'limit & IN/ALL/ANY/SOME subquery'

You can see it IN detail. IN does not support LIMIT. What should we do? Www.jbxue.com

Then du Niang learned that she would use a temporary table IN the IN statement to find out the required content first,

After modification:
SELECT infor. name, infor. phone, infor. add,
Mconfig. mname, mapply. acount, from_unixtime (mapply. atime, '% Y-% m-% D') as 'atime'
FROM mapply right JOIN infor ON mapply. uid = infor. uid inner JOIN mconfig ON mapply. mid = mconfig. mid
WHERE mapply. aid
IN (
SELECT aid
FROM (SELECT 'aid 'FROM 'mapply' where state = $ state
Order by 'atime ', 'uid' DESC
LIMIT) AS 'tp'
)
After running, the problem is solved!

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.