我有以下查询成功生成结果。但是,结果是顺序的,我想在两个名为clicks
(SELECT women_id, title, body FROM women ORDER BY women_id DESC LIMIT 5)
UNION ALL
(SELECT man_id, title, body FROM man ORDER BY man_id DESC LIMIT 5)
现在它给出了这样的结果:
women 41124
women 124
women 12
women 2
women 1
men 3455434
men 1233
men 135
men 23
men 1
我需要它来给出这样的最终结果:
men 3455434
women 41124
men 1233
men 135
women 124
men 23
women 12
women 2
women 1
men 1
答案 0 :(得分:5)
您需要将clicks
引入UNION
- ed数据集,添加ORDER BY
,然后将不必要的列与其他SELECT
一起投影,就像这样:
SELECT person_id, title, body FROM ((
(SELECT women_id as person_id, title, body, clicks FROM women ORDER BY women_id DESC LIMIT 5)
UNION ALL
(SELECT man_id as person_id, title, body, clicks FROM man ORDER BY man_id DESC LIMIT 5)
) ORDER BY clicks DESC) data
答案 1 :(得分:0)
(
(SELECT women_id, title, body, clicks FROM women ORDER BY women_id DESC LIMIT 5)
UNION ALL
(SELECT man_id, title, body, clicks FROM man ORDER BY man_id DESC LIMIT 5)
)
ORDER BY clicks DESC
只需在最后添加订单并完成工作
答案 2 :(得分:0)
SELECT * FROM
(
(SELECT women_id AS ID, title, body FROM women LIMIT 5)
UNION ALL
(SELECT man_id AS ID, title, body FROM man LIMIT 5)
)
ORDER BY ID DESC