简体   繁体   中英

Using order by DESC or ASC in mysql queries with UNION keyword

In my page I used the following query for displaying choices starts from current time to old time of choices. But it selects the row starts from old time choices to current time choices.

I want to display the choices starts from current time to old one

$result=mysql_query("(SELECT choice_id,poll_id,choicecreationtime from je_addchoice ORDER BY choicecreationtime DESC)
    UNION
    (SELECT choice_id,poll_id,datetime_voted from je_user_vote ORDER BY datetime_voted DESC)");

I dont know why this problem occurs. If I don't use the UNION means it works fine. For example. The below query shows the order of current time to old

 $result = mysql_query("SELECT * FROM je_addchoice, je_addpoll where je_addpoll.privacy='0' AND je_addpoll.start_date <= '$check_date' AND je_addpoll.end_date >='$check_date'  AND je_addpoll.poll_id=je_addchoice.poll_id order by je_addchoice.choicecreationtime desc");

Anybody can help me to solve this problem

wrap the union ed queries by subquery and order them on the outer query

SELECT *
FROM 
    (
        SELECT choice_id, poll_id, choicecreationtime AS TIME
        FROM je_addchoice
        UNION
        SELECT choice_id, poll_id, datetime_voted AS TIME
        FROM je_user_vote
    ) s
ORDER BY TIME DESC

UPDATE 1

SELECT choice_id, poll_id, choicecreationtime AS TIME
FROM je_addchoice
UNION
SELECT choice_id, poll_id, datetime_voted AS TIME
FROM je_user_vote
ORDER BY Time DESC

As documented under UNION Syntax :

To apply ORDER BY or LIMIT to an individual SELECT , place the clause inside the parentheses that enclose the SELECT :

 (SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10) UNION (SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10); 

However, use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows. Therefore, the use of ORDER BY in this context is typically in conjunction with LIMIT , so that it is used to determine the subset of the selected rows to retrieve for the SELECT , even though it does not necessarily affect the order of those rows in the final UNION result. If ORDER BY appears without LIMIT in a SELECT , it is optimized away because it will have no effect anyway.

To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one. The following example uses both clauses:

 (SELECT a FROM t1 WHERE a=10 AND B=1) UNION (SELECT a FROM t2 WHERE a=11 AND B=2) ORDER BY a LIMIT 10; 

A statement without parentheses is equivalent to one parenthesized as just shown.

Try this:

SELECT choice_id, poll_id, dt
FROM (SELECT choice_id, poll_id, choicecreationtime dt FROM je_addchoice 
      UNION 
      SELECT choice_id, poll_id, datetime_voted dt FROM je_user_vote ) AS A
ORDER BY dt DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM