Sohaib Rahman
Sohaib Rahman

Reputation: 183

how to use order by based on strings in sqlite

i have a dataset in sqlite db which has ids and image urls. It is as follows:

ID  |   URL
1   |   http://img.url1.com
2   |       
3   |   http://img.url2.com
4   |   http://img.url3.com
5   |   http://img.url4.com
6   |   
7   |   http://img.url5.com
8   |   
9   |   http://img.url6.com
10  |   
11  |   http://img.url7.com
12  |   http://img.url8.com
13  |   

I want to sort this accordingly in such a way that the ids with blank values appears at the bottom and the ids with nearest with img urls get appeared at the top with nearest values in the sorted order of ids as follows:

ID  |   URL
1   |   http://img.url1.com
3   |   http://img.url2.com
4   |   http://img.url3.com
5   |   http://img.url4.com
7   |   http://img.url5.com
9   |   http://img.url6.com
11  |   http://img.url7.com
12  |   http://img.url8.com
2   |       
6   |   
8   |   
10  |   
13  |   

Can you suggest me what kind of query I need to put ?

Upvotes: 1

Views: 65

Answers (1)

valex
valex

Reputation: 24144

SELECT * FROM T
ORDER BY (URL IS NULL),id

SQLFiddle demo

Upvotes: 2

Related Questions