need
pagehelper pagination query: table fu_plan has field follow_up_status, the value of this field can be 1 or 2.
The conditions are as follows
1、值为2的数据在最前面,值为1的值在后面
2、值为2的数据按字段start_time ,create_time 都倒序排列
3、值为1的数据按字段start_time正序 ,create_time 倒序排列
train of thought
1. First find out the corresponding data according to the value of follow_up_status and sort them.
2. Merge the results of the two subqueries
3. According to the merged results, sort them in reverse order of follow_up_status
Complete sql first
SELECT * FROM
(SELECT fp.* FROM
fu_plan fp JOIN hm_project_customer hp on fp.archive_id = hp.health_archive_id
and hp.customer_status = 1
WHERE
fp.archive_id = 404
and fp.follow_up_status = 1
ORDER BY fp.start_time desc,fp.create_time desc
LIMIT 0,10
) as t1
UNION
(SELECT fp.* FROM
fu_plan fp JOIN hm_project_customer hp on fp.archive_id = hp.health_archive_id
and hp.customer_status = 1
WHERE
fp.archive_id = 404
and fp.follow_up_status = 2
ORDER BY fp.start_time asc,fp.create_time desc
LIMIT 0,100
)
ORDER BY follow_up_status desc;
Pit 1
When using UNION, if you do not add parentheses to the two query results, an error will be reported. If you add the first and second subqueries without adding "alias", an error will be reported.
1248 - Every derived table must have its own alias
First, add as xx "alias" to both subqueries and report an error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as t2
ORDER BY follow_up_status desc' at line 27
Pit 2
The first addition, the second subquery does not add "alias" and the result is successfully queried, but there is a problem with the sorting
Solution:
Add in the subquery, the limit keyword takes effect