SQL uses a union sorting problem

Source: Internet
Author: User

Original: 65628037

The recent union query using SQL statements, surprised to find: SQL is not a problem, the union query is not a problem, you can get the desired results, but in the order of the results, but there is a problem.

1.UNION Query No problem

  1. SELECT ' id ', 'username ',' mobile ',' time ',ID as leader
  2. from ' Grouporder_leader '
  3. WHERE ' courseid ' = + ' merchid ' = All and ' status ' = 1
  4. UNION All
  5. SELECT Leadorderid,username,mobile, time,null
  6. from ' Grouporder_partner '
  7. WHERE courseid=and status=1 and merchid=

The results are as follows

2. The sorting is going to be a problem.

  1. SELECT ' id ', 'username ',' mobile ',' time ',ID as leader
  2. from ' Grouporder_leader '
  3. WHERE ' courseid ' = + ' merchid ' = All and ' status ' = 1
  4. ORDER by Time DESC
  5. UNION All
  6. SELECT Leadorderid,username,mobile, time,null
  7. from ' Grouporder_partner '
  8. WHERE courseid=and status=1 and merchid=
  9. ORDER by Time DESC

Error after executing this SQL statement.

3. Create a temporary table

Save the query results using a method similar to creating a temporary table, and then sort the query on the staging table.

  1. SELECT id,username,mobile,time,leader
  2. From (SELECT ' id ', 'username ', 'mobile ',' time ',ID as leader
  3. from ' Grouporder_leader ' WHERE ' courseid ' = + ' merchid ' = All and ' st ATUs ' = 1
  4. UNION All
  5. SELECT Leadorderid,username,mobile, time,null
  6. from ' Grouporder_partner ' WHERE courseid=and status=1 and merchid=
  7. )
  8. ORDER by Time DESC

4. Aliases

Do not know why the 3rd step in the query is still not, and then the result of the union query the individual name, and then query the sort is no problem.

  1. SELECT A.id,a.username,a.mobile,a.time,a.leader
  2. From (SELECT ' id ', 'username ', 'mobile ',' time ',ID as leader
  3. from ' Grouporder_leader ' WHERE ' courseid ' = + ' merchid ' = All and ' s Tatus ' = 1
  4. UNION All
  5. SELECT Leadorderid,username,mobile, time,null
  6. from ' Grouporder_partner ' WHERE courseid=and status=1 and merchid= /c11>
  7. ) as a
  8. ORDER by Time DESC

The result is correct.

Find out to say, and then go to heavy or other operations, also no problem.

    1. SELECT DISTINCT a.id,a.username,a.mobile,from_unixtime (a.time,'%y/%m/%d ') as _time,a.leader
    2. From (SELECT ' id ', 'username ', 'mobile ',' time ',ID as leader
    3. from ' Grouporder_leader ' WHERE ' courseid ' = + ' merchid ' = ' status ' = C10>1
    4. UNION All
    5. SELECT Leadorderid,username,mobile, time,null
    6. from ' Grouporder_partner ' WHERE courseid=and status=1 and merchid=
    7. ) as a
    8. ORDER by Time DESC

SQL uses a union sorting problem

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.