PDA

View Full Version : group by and order by


kamkam
08-18-2009, 10:34 AM
Hi;

I have a table for rentting house



create table rent(id smallint auto_increment,
area varchar(200),
content text,
price varchar(200);
submittime TIMESTAMP Default CURRENT_TIMESTAMP,
primary key(id)

)

How i can display the result order by area as ASC, and if the areas are same,
they will order by price as ASC ?

for example

1)
area: abe
price $9
content: xxxxxx

2)

area: abe
price $19
content: xxxxxx

3)

area: abe
price $29
content: xxxxxx


4)

area: dbe
price $29
content: xxxxxx


5)

area: dbe
price $39
content: xxxxxx


6)

area: kbe
price $39
content: xxxxxx


i just can do the following, could anyone help me, please.

select * from rent order by area asc

abduraooft
08-18-2009, 11:45 AM
select area,price,content from rent order by area,price ASC ?

kamkam
08-18-2009, 12:02 PM
Thank you very much

bazz
08-18-2009, 02:49 PM
the table create statement could do with a clean up.


create table rent
( id smallint auto_increment primary key
, area varchar(200)
, content text
, price decimal(7,2)
, submittime TIMESTAMP Default CURRENT_TIMESTAMP
) engine=innodb default charset=latin1;


You can change innodb and latin1 to whatever you need them to be. Notice decimal is chosen for price. If area is meant to be a suburb or a locality of some sort, then it should probably be better as varchar (64) or something.

hth

bazz