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

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.

Related

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;

MySQL order by ASC and DESC in same column

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.

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;

How to sort rows by ON, OFF, SOLD

I have followings 3 values that could be in a row in database - ON, OFF, SOLD (column sort_it). When I set the sort clausule on ORDER BY sort_it ASC, so I will get the items with a value in the sort_in column OF, then ON and SOLD.
But I need the sequence ON, OFF, SOLD.
Exist any way to do it somehow? I mean... edit a way saving data into database will be demanding, so I would do this in the last moment.
Yes you can use custom data sortings like this:
SELECT * FROM table ORDER BY FIELD(`sort_it`,'ON','OFF','SOLD'),`sort_it`
You can use a CASE statement to generate your custom ordering sequence from the underlying values, something like this:
ORDER BY
CASE sort_it
WHEN 'ON' then 1
WHEN 'OFF' then 2
WHEN 'SOLD' then 3
END
Sample Demo: http://sqlize.com/MGz6lLb0Qk
Using Strings is generally a bad idea. it would be better to use a numeric type. Then you can say ON=1, OFF=2 and SOLD=3 and then sort.
SELECT t.*, IF(sort_it='ON',1,IF(sort_it='OFF',2,3)) as new_sort FROM Table AS t ORDER by new_sort ASC
Another solution from comments on MySql manual:
http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html
select * from tablename order by priority='High' DESC, priority='Medium' DESC, priority='Low" DESC;

issue with ORDERING query results

I currently construct an array of ids and query this array using implode like so:
$sql = "SELECT * FROM item_bank_tb WHERE item_id IN(" . implode(',', $ids) . ")";
the array $ids is constructed in such a way that the ids are in a specific order. However the results of this query are not in that order. I'm guessing as they are all in one query the results appear in the order they were located (ascending).
Is there a way of getting around this? (other than including a field which i can ORDER BY)
many thanks.
Take a look at this example. You have to use field() function.
SELECT * FROM item_bank_tb WHERE item_id IN(1,3,2)
order by field(item_id,1,3,2)
in this way you can get your items in your desired order.
Seriously, what's the problem with adding ORDER BY item_id
If you want the rows of a SQL query returned in a specific order, you must include an ORDER BY in the query.
Use ORDER BY to sort your results. You can either have ORDER BY DESC (descending values from higher to lower) or ORDER BY ASC (ascending from lower to higher).
For example:
SELECT colName,colEmail FROM tblUsers ORDER BY id ASC
Read more here:
http://www.w3schools.com/sql/sql_orderby.asp
you can construct an "orderer" temporay table (if it has not too much data) instead of the id array you can insert to the temp table two values: ID, order
and then join and then query with "order by order"

Categories