Irene Ling
Irene Ling

Reputation: 1981

SQL php Select priority

If I have 2 type of data in a table, which is either ID1 or ID2 empty, how can I select the rows that ID1 is not empty and also is the latest inserted row first , then follow by the data with ID2 not empty?

Example:

 Data | ID1 | ID2

 1    | ok  | 

 2    |     |  ok

 3    | ok  |

and returns:

Data | ID1 | ID2

 3   | ok  |

 1   | ok  | 

 2   |     |  ok

Thank you very much.

Upvotes: 0

Views: 107

Answers (3)

Waygood
Waygood

Reputation: 2693

If ID1 and ID2 are 'ok' or empty then Mahmoud's answer is correct. If not, say its an ID from another table then you can do this:-

SELECT * 
FROM tablename
ORDER BY ID1='' DESC, ID2='' DESC, Data DESC

Upvotes: 1

Madhivanan
Madhivanan

Reputation: 13700

SELECT * 
FROM tablename
ORDERBY ID1 DESC, ID2 

Upvotes: 1

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79919

Try this:

SELECT * 
FROM tablename
ORDERBY ID1 DESC, ID2 DESC

Upvotes: 1

Related Questions