union、union all 排序问题,子查询排序条件会被优化器干掉

union、union all 排序问题,子查询排序条件会被优化器干掉:

例如:

(SELECT customer_status,create_time FROM customer t1 WHERE t1.customer_status='4' AND t1.is_deleted=0 ORDER BY t1.create_time DESC )

UNION ALL

(SELECT customer_status,create_time FROM customer t2 WHERE t2.customer_status!='4' AND t2.is_deleted=0 ORDER BY t2.create_time DESC )

union或者union all的子查询排序条件会被优化器干掉,排序条件create_time是无效的,应该加上limit,改为:

(SELECT customer_status,create_time FROM customer t1 WHERE t1.customer_status='4' AND t1.is_deleted=0 ORDER BY t1.create_time DESC limit 1000 )

UNION ALL

(SELECT customer_status,create_time FROM customer t2 WHERE t2.customer_status!='4' AND t2.is_deleted=0 ORDER BY t2.create_time DESC limit 1000)

你可能感兴趣的:(union、union all 排序问题,子查询排序条件会被优化器干掉)