两个sql查询使用union之后排序失效的解决方法

(
	SELECT
		*
	FROM
		(
			SELECT
				a.id,
				a.userId,
				a.infoId,
				a.isRead,
				a.infoType,
				b.title,
				b.createTime
			FROM
				c_user_info a
			LEFT JOIN c_info b ON a.infoId = b.id
			WHERE
				1 = 1
			AND isRead = '0'
			AND userId = '1'
			ORDER BY
				b.createTime DESC
		) AS activity1
)
UNION 
	(
		SELECT
			*
		FROM
			(
				SELECT
					a.id,
					a.userId,
					a.infoId,
					a.isRead,
					a.infoType,
					b.title,
					b.createTime
				FROM
					c_user_info a
				LEFT JOIN c_info b ON a.infoId = b.id
				WHERE
					1 = 1
				AND isRead = '1'
				AND userId = '1'
				ORDER BY
					b.createTime DESC
			) AS activity2
	);
解决方法:在每一个sql语句都是使用select  * from (sql语句) as  activity  ,将activity  进行union操作。

你可能感兴趣的:(两个sql查询使用union之后排序失效的解决方法)