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
- SELECT ' id ', 'username ',' mobile ',' time ',ID as leader
- from ' Grouporder_leader '
- WHERE ' courseid ' = + ' merchid ' = All and ' status ' = 1
- UNION All
- SELECT Leadorderid,username,mobile, time,null
- from ' Grouporder_partner '
- WHERE courseid=and status=1 and merchid=
The results are as follows
2. The sorting is going to be a problem.
- SELECT ' id ', 'username ',' mobile ',' time ',ID as leader
- from ' Grouporder_leader '
- WHERE ' courseid ' = + ' merchid ' = All and ' status ' = 1
- ORDER by Time DESC
- UNION All
- SELECT Leadorderid,username,mobile, time,null
- from ' Grouporder_partner '
- WHERE courseid=and status=1 and merchid=
- 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.
- SELECT id,username,mobile,time,leader
- From (SELECT ' id ', 'username ', 'mobile ',' time ',ID as leader
- from ' Grouporder_leader ' WHERE ' courseid ' = + ' merchid ' = All and ' st ATUs ' = 1
- UNION All
- SELECT Leadorderid,username,mobile, time,null
- from ' Grouporder_partner ' WHERE courseid=and status=1 and merchid=
- )
- 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.
- SELECT A.id,a.username,a.mobile,a.time,a.leader
- From (SELECT ' id ', 'username ', 'mobile ',' time ',ID as leader
- from ' Grouporder_leader ' WHERE ' courseid ' = + ' merchid ' = All and ' s Tatus ' = 1
- UNION All
- SELECT Leadorderid,username,mobile, time,null
- from ' Grouporder_partner ' WHERE courseid=and status=1 and merchid= /c11>
- ) as a
- ORDER by Time DESC
The result is correct.
Find out to say, and then go to heavy or other operations, also no problem.
- SELECT DISTINCT a.id,a.username,a.mobile,from_unixtime (a.time,'%y/%m/%d ') as _time,a.leader
- From (SELECT ' id ', 'username ', 'mobile ',' time ',ID as leader
- from ' Grouporder_leader ' WHERE ' courseid ' = + ' merchid ' = ' status ' = C10>1
- UNION All
- SELECT Leadorderid,username,mobile, time,null
- from ' Grouporder_partner ' WHERE courseid=and status=1 and merchid=
- ) as a
- ORDER by Time DESC
SQL uses a union sorting problem