sasy
sasy

Reputation: 583

Group data Set with OrderBy in Sql

id   name   amount
1    A      12
2    B      10
3    A      24
4    C      39
5    B      25
6    B      23
7    C      15

i want to get data like this

id   name   amount
1    A      12
2    A      24
3    B      10
4    B      23
5    B      25
6    C      15
7    C      39

i want to get the data like this -- set of A with the ascending amount and the B with the ascending amount and the C how to do that i tried this code but it only return 3 values (i'm using laravel)

DB::table('person')             
->groupBy('name')
->orderBy('amount','ASC')
->get();

Upvotes: 1

Views: 28

Answers (4)

Someshwer Bandapally
Someshwer Bandapally

Reputation: 149

You can try as follows

$persons = DB::table('person')             
->orderBy('amount','asc')
->get();

$result = $persons->groupBy('name');

return $result;

I think this works fine.

Upvotes: 0

Rogin Thomas
Rogin Thomas

Reputation: 772

Try this,

DB::table('person')->orderBy(array('amount'=>'asc', 'name'=>'asc'))->get();

Upvotes: 0

Afsar
Afsar

Reputation: 3124

you can use orderBy() many time at any query

DB::table('person')
->orderBy('amount','ASC')
->orderBy('name', 'ASC')
->get();

Upvotes: 1

Charanjeet Singh
Charanjeet Singh

Reputation: 145

Do not use group by.Use only orderby. First with name than amount. It will work.

Upvotes: 0

Related Questions