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

次のように、clicksUNION-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

最後に order by を追加するだけで仕事は完了です

于 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 に答える