MySQL order by ASC and DESC in same column - php

In MySQL is there a way to sort a column DESC for the purpose of GROUP BY but then sort that same column again ASC for the actual output? If not, then how would you recommend I re-sort them in PHP when I'm handling the results?

GROUP BY doesn't care about your order, it's running over the whole dataset anyway.

Related

MySQL - Sort multiple columns together; not first, then second, etc

I have a table with 4 different date columns (date_created, date_saved, date_published, date_deleted), and I'm trying to do a "most recent updates" query, effectively taking any of those 4 dates into consideration and sorting by the most recent dates desc. Normally you'd do:
date_created DESC, date_saved DESC, date_published DESC, date_deleted DESC
But I don't want it to give more importance to date_created than date_deleted. All the date fields are an "update" so they should all be treated equally in the sorting.
I'd rather not select all the results in the whole database and then use PHP to sort and limit for resources sake, so, is there a way sort DESC on all of those 4 fields, while treating them all as equal importance?
simply do:
ORDER BY GREATEST(date_created, date_saved, date_published, date_deleted)

How do I retrieve data and present it earliest in date order through PHP?

I want to get data from my database and present each field in separate divs - which I can do. However it is ordered earliest to latest meaning the first bits of data are at the top. How do I get it the other way round? Is it something I have to do in phpMyAdmin?
I am a Noob, please help. (thanks in advance).
Without seeing your actual SQL statements and tables its hard to give a definitive answer.
However, you're looking for the ORDER BY clause. You can order something by a field in either Ascending (1, 2, 3) or Descending (3, 2, 1) Order.
For example:
SELECT * FROM Employees ORDER BY HireDate ASC
vs
SELECT * FROM Employees ORDER BY HireDate DESC
To get it in reverse order.
From what I have understood is that you want to display the data in descending order. Use ORDER BY clause, which helps you to sort the data either in ascending or descending order.
for example:
SELECT * FROM shop
ORDER BY date DESC;

Sub Queries in an ORDER BY clause MySQL

Im writing a module for an e-commerce platform that modifies the ORDER BY clause of an MySQL query.
I only have access to anything after the ORDER BY statment.
Im wanting to execute ORDER BY statements on calculated columns as such.
" ORDER BY (SELECT (shop_product.price - shop_product.sale_price) as pSavings) ASC "
Although i have no idea how i would go about this or if its even possible as i dont have access to the SELECT statement to generate the calculated columns?
If you mean this (then it is possible):
ORDER BY (shop_product.price - shop_product.sale_price) ASC
You can Try with this.
SELECT (shop_product.price - shop_product.sale_price) as pSavings ORDER BY pSavings ASC;

Its possible Reverse mysql fetch array with out ORDER By clauses?

Its possible to reverse mysql_fetch_array values,without using ORDER BY clauses in SELECT query?
If what you want is just to reverse the returned array, then you can use array_reverse:
array_reverse($array);
You can find more information at the documentation.
When you do a select statement, you can never be certin of the order of items returned. So in order to be certain, you must use a order by clause. If you want a reverse order, do
ORDER BY columnA DESC
Most of the time, most databases will return things, without an order by in the order that rows were added, but it is non-deterministic.

Select and sort on two columns MySQL

So, my code looks like this
mysql_query("SELECT * FROM threads WHERE forum_id = '$id' ORDER BY type,posted DESC") or
die(mysql_error());
"posted" is the value of time() and is selected decreased to put the latest one at the top.
"type" contains either 1 or 2. If it's 2, the thread is pinned. Currently, it sorts after posted but those which should be at the top (pinned) are at the bottom. Is there any fix that I'm missing?
Try: ORDER BY type DESC, posted DESC
By default, it sorts ascending. You need to specify the order for both fields you would like to order by.
ORDER BY Documentation
Don't forget that the asc/desc in an order by applies to each individual field, not the entire order by clause.
ORDER BY type DESC, posted ASC
MySQL can also accept arbitrary logic for order by, so for more complicated sort requirements, you could have something like
ORDER BY IF(type=2, 0, 1) AS pinned ASC, posted DESC
as long the arbitrary logic returns something that's trivially sortable (e.g. a number or a string), there's no limit to how complicated the sorting can be.

Categories