Note that there are some explanatory texts on larger screens.

plurals
  1. POOrder by date and auto increment column (id), but order rows with same value for third column (oid) alphabetically by fourth column (ab)
    primarykey
    data
    text
    <p>Following Quassnoi's answer I have edited to show what happens using his advice and added letters to the OID to show the problematic ordering.</p> <p>I have a table in a mysql database that I first want to order by the date it was supposed to be added to the database.</p> <pre><code>SELECT * FROM table ORDER BY date_placed DESC; </code></pre> <p>Next where the date is the same I want to order by an auto incremented id. So I've added:</p> <pre><code>SELECT * FROM table ORDER BY date_placed DESC, id DESC; </code></pre> <p>I end up with something like this:</p> <pre> Date_Placed | ID | OID | AB 05/01/2012 | 1100 | A50 | A 05/01/2012 | 1109 | A50 | B 05/01/2012 | 1108 | B40 | A 05/01/2012 | 1107 | B40 | B 04/01/2012 | 1106 | C30 | A 04/01/2012 | 1105 | C30 | B </pre> <p>The AB field will always only by A or B, and OID may either exist twice or be a unique value. What I want now is in those cases where OID is duplicated, for B to come before A in the AB column. Note that ID will then be out of order to an extent.</p> <pre> Date_Placed | ID | OID | AB 05/01/2012 | 1109 | A50 | B 05/01/2012 | 1110 | A50 | A 05/01/2012 | 1107 | B40 | B 05/01/2012 | 1108 | B40 | A 04/01/2012 | 1105 | C30 | B 04/01/2012 | 1106 | C30 | A </pre> <p>I'd prefer to do this on the mysql side since I'll be retrieving quite a few rows. Is this possible? If not in mysql then I am using php so what would be the best way to do it in that? Thank you in advance to anyone who can help.</p> <p>Quassnoi's answer gives this:</p> <pre> Date_Placed | ID | OID | AB 05/01/2012 | 1107 | B40 | B 05/01/2012 | 1108 | B40 | A 05/01/2012 | 1109 | A50 | B 05/01/2012 | 1110 | A50 | A 04/01/2012 | 1105 | C30 | B 04/01/2012 | 1106 | C30 | A </pre> <p>ID order is lost</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload