ORDER by two column problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

ORDER by two column problem

Post by ssand »

Hi,

I am trying to ORDER by two columns.

Code: Select all

$inventory_list = "SELECT * FROM vehicles, inv_type
								WHERE inv_type.inv_type_name = '$_GET[get_inventory]'
								AND vehicles.inv_type_id = inv_type.inv_type_id
								AND vehicles.vehicle_active = 1
								ORDER by vehicles.$orderby, vehicles.$orderby2 $sortby
								LIMIT $offset, $rowsPerPage";
The results are correct except it seems to only be sorting by the first value (vehicles.$orderby).
If I echo the values of $orderby and $orderby the values are as expected.

If I take the:

Code: Select all

ORDER by vehicles.$orderby, vehicles.$orderby2 $sortby
and replace with:

Code: Select all

ORDER by year, price $sortby
the results are still only sorted by the year.

Thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Output the query and see what it looks like. $sortby might not be what you expect.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Post by ssand »

Well, the query looked ok on output. As coded before.

I went and changed the ORDER by to

Code: Select all

ORDER by vehicles.$orderby $sortby, vehicles.$orderby2 $sortby
And it seems to sort correctly.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

ssand wrote:The results are correct except it seems to only be sorting by the first value (vehicles.$orderby).
If I echo the values of $orderby and $orderby the values are as expected.
This will not be true. If you are first field in place, then the results will be sorted in asc or desc and asc by default. You might not have recognized it.
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Post by ssand »

raghavan20 wrote:
ssand wrote:The results are correct except it seems to only be sorting by the first value (vehicles.$orderby).
If I echo the values of $orderby and $orderby the values are as expected.
This will not be true. If you are first field in place, then the results will be sorted in asc or desc and asc by default. You might not have recognized it.
I think you may be absolutely correct. :oops:
Post Reply