4

我有以下查询成功产生结果。但是,结果是连续的,我想按两个表中名为的列对它们进行排序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
4

3 回答 3

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
于 2013-06-11T17:17:38.383 回答
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

只需在最后添加订单并完成工作

于 2013-06-11T17:17:10.060 回答
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
于 2013-06-11T17:18:03.267 回答