UNION
is used to combine the result from multiple SELECT
statements into a single result set.
The column names from the first SELECT
statement are used as the column names for the results returned. Selected columns listed in corresponding positions of each SELECT
statement should have the same data type.
SELECT
*
FROM
(
( SELECT material_article_id, `index`, user_id, readed, read_times
FROM msg_article_user_record a
WHERE a.publish_base_id = 157 AND a.readed = 1
ORDER BY a.`index`
)
UNION ALL
( SELECT material_article_id, `index`, user_id, readed, read_times
FROM msg_article_user_record b
WHERE b.publish_base_id = 157 AND b.readed = 0
ORDER BY b.`index`
)
) as r
Attention: 三地地方的表别名必不可少,缺少时报错:Every derived table must have its own alias.