0

I have a table with following schema. I need to sort table by points, and if rows wiith img_link present (not null) come first also. Simply need to do is - sort by int column, then by varchar column.

+-----+--------+-----------+-----------+
| id  | name   | img_link  |  points   |
+-----+--------+-----------+-----------+
| 11  | smpl   | path.jpg  |  10       |
+-----+--------+-----------+-----------+
| 12  | main   |  null     |  20       |
+-----+--------+-----------+-----------+
| 13  | abcd   |  null     |  10       |
+-----+--------+-----------+-----------+
| 14  | xyls   | img_.png  |  10       |
+-----+--------+-----------+-----------+

Need a result like

+-----+
| id  |
+-----+
| 12  |
+-----+
| 11  |
+-----+
| 14  |
+-----+
| 13  |
+-----+
1
  • "sort by int column , then by varchar column" And have you actually tried that? Commented Apr 23, 2014 at 13:00

5 Answers 5

2

Try This

SELECT * FROM table_name ORDER BY points DESC ,ISNULL(img_link), img_link 
Sign up to request clarification or add additional context in comments.

Comments

1

You basically wrote out in words exactly what you need to do.

SELECT id FROM someTable ORDER BY points DESC, img_link DESC;

DEMO

Comments

1

Another way is

select *
from table
order by `points` desc,
if(img_link = '' or img_link is null,1,0)

DEMO

Comments

0

try this:

select * from tabalename where img_link is not null order by point desc union select * from tabalename where img_link is null order by point desc

Comments

0
SELECT * FROM my_table ORDER BY points DESC, img_link IS NULL, img_link DESC;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.