Jump to content


Photo

Need help setting up an ORDER BY


  • Please log in to reply
3 replies to this topic

#1 Hostile

Hostile

    Benefitting Humanity Simply by Showing Up!

  • Veterans
  • 9,551 posts
  • Location:Washington DC
  •  T3A Founder
  • Division:Revora
  • Job:Global Administrator
  • Donated
  • Association

Posted 22 September 2010 - 07:04 PM

I'm trying to take somebody else's code who used to work my job but is gone and add a sort order. Right now it's ascending by date. I need it to descend.

$Query='select * from '.$Category.' where isAvailable<>""';

Normally this would be done by adding this
SELECT * FROM table_name
ORDER BY Date DESC

I tried this but it didn't work.
$Query='select * from '.$Category.' ORDER BY Date DESC where isAvailable<>""';

I know it's something simple as I'm not the best coder around. Any help will do.

#2 Beowulf

Beowulf

    Shipgirl

  • Advisors
  • 7,219 posts
  •  Azur Lane Fangirl

Posted 23 September 2010 - 03:22 AM

The WHERE clause has to precede the ORDER BY. In short, move the ORDER BY to the end of the statement.

NZ.org | BBPCG
Discord: The Astronomer#1314
Steam


#3 Bart

Bart

  • Network Admins
  • 8,524 posts
  • Location:The Netherlands
  • Division:Revora
  • Job:Network Leader

Posted 23 September 2010 - 02:37 PM

As a general rule, clauses in the query must be given in the order they will be executed (SELECT is an exception), so:

- You need to filter the result before you can sort it -> WHERE comes before ORDER
- Grouping must be done after filtering -> WHERE comes before GROUP BY
- HAVING lets you add another filter after grouping has been done -> HAVING comes after GROUP BY
- Only after you've filtered, grouped and whatnot can you determine what the first X rows are -> LIMIT comes last
- etc...
bartvh | Join me, make your signature small!
Einstein: "We can’t solve problems by using the same kind of thinking we used when we created them."

#4 Jeeves

Jeeves

    I write the interwebz

  • Members
  • 4,156 posts
  •  Friendly neighborhood standards Nazi

Posted 26 September 2010 - 05:00 AM

Or WOGHL for short, which doesn't make it any easier to remember :p
Also, if you have access to phpMyAdmin, use it. Echo your query to check the variables are appearing right, then copy-paste to phpMyAdmin's query box and you can easily see any errors and try changes faster than through editing the PHP.

World Domination Status: 2.7%





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users