Skip to content

Instantly share code, notes, and snippets.

@latompa
Created January 13, 2009 01:07
Show Gist options
  • Save latompa/46273 to your computer and use it in GitHub Desktop.
Save latompa/46273 to your computer and use it in GitHub Desktop.
-- You have a products table, and want to show the ones a user can afford first sorted on price
-- followed by the products he can't afford, also sorted
create table products(id integer, name varchar(32), price integer)
insert into products values
(1,"teddy bear",10),
(2,"red fire engine",5),
(3,"doll house",50),
(4,"lego castle",190),
(5,"horse ranch",250),
(6,"tea set",75)
select *,(price<=100) as afford from products order by afford desc, price desc
-- yields
+------+-----------------+-------+--------+
| id | name | price | afford |
+------+-----------------+-------+--------+
| 6 | tea set | 75 | 1 |
| 3 | doll house | 50 | 1 |
| 1 | teddy bear | 10 | 1 |
| 2 | red fire engine | 5 | 1 |
| 5 | horse ranch | 250 | 0 |
| 4 | lego castle | 190 | 0 |
+------+-----------------+-------+--------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment