issue with ORDERING query results - php

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"

Related

SQL order by length for multiple columns

I want to sort by order by length in multiple columns. My code is working for one column, but it's not working with multiple columns.
This is working
$query = $wpdb->get_results("SELECT id,demo1,demo2 FROM $table_name ORDER BY LENGTH(demo1) DESC");
It's not working
$query = $wpdb->get_results("SELECT id,demo1,demo2 FROM $table_name ORDER BY LENGTH(demo1),LENGTH(demo2) DESC");
ORDER BY LENGTH(demo1), demo1 DESC,
LENGTH(demo2), demo2 DESC
The results are ordered by the first column, then the second, and so on for as many columns as the ORDER BY clause includes. If you want any results sorted in descending order, your ORDER BY clause must use the DESC keyword directly after the name or the number of the relevant column.
If you have not understood properly..and
Before voting down my answer... Can I see your table result please?

Select data from mysql in different order

I have data along with the ids 1,2,3,4,5 but these are not primary ids...
I want to fetch data in order like 4,3,2,1,5.
I cann't change the ids in database.If i use order by desc it will fetch 5,4,3,2,1.
you can use FIELD in order clause
order by FIELD(field_name,4,3,2,1,5)
Order by field is one such way:
select * from TABLE order by FIELD(column_name,4,3,2,1,5) ;
You can look into this for more details:
Field Example
you can use ORDER BY RAND() for random order.
Try this,
SELECT table.* FROM table ORDER BY FIELD(id,4,3,2,1,5);

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.

How to select from last to first row in mysql

Whenever a query like
"select * from table where userid=xx" is done, the mysql fetches these values from
first row to last row of table.
But I want to select from last to first so that recently updated values are displayed first in the results.
I cannot do "select * from table where userid=xx order by time DESC" because there is no time column in table.
I just want recently updated items in the table displayed first.
$result= mysql_query("SELECT
(SELECT column FROM table WHERE [condition] ORDER BY column LIMIT 1) as 'first',
(SELECT column FROM table WHERE [condition] ORDER BY column DESC LIMIT 1) as 'last'");
$row=mysql_fetch_array($result);
echo $row['first'];
echo $row['last'];
If you have any auto-incrementing field you could sort by that desc.
You must provide a column to order by in order to guarantee results. So you will have to either change your table structure or make a decision on what to order by.
A hack would be to pull in the data in the order presented into an array, then start popping off the bottom of that array.
You either have to have a timestamp, or autoincrement id, or some other column that you want to sort by.
Just because you get rows in a certain order from a database when not using an ORDER BY clause, does not mean that they are guaranteed to be returned in that order. It also doesn't imply any order in the result set. You need a definitive field that you can use to ORDER BY, or you cannot do what you are wanting to do.
Some hack you can do for that if you don't have columns you can rely on:
SELECT * FROM (SELECT *,(#x:=#x+1) default_ordering FROM `table_name`, (SELECT #x:=0) t2) any_name ORDER BY default_ordering DESC

IDs not sorting

I got an odd problem. Whenever I add a new entry to the database it gives it a higher id number, but somehow sees it as a lower number and puts it below the older ones.
And when sorting DESC in PHP I also get this order, does anybody know whats going wrong here?
I use $res = mysql_query("SELECT * FROM data ORDER BY 'id' DESC");to sort them, and it gives the same order as in the pic. (Not sure why i'm being downvoted here but ok..)
Pic:
Your query is:
SELECT * FROM data ORDER BY 'id' DESC
You are sorting by the string 'id', note the syntax highlighting when not within quotes. This means you get them in a random order. Remove the '. If you meant to escape the column id you should use back-ticks:
SELECT * FROM data ORDER BY `id` DESC
When you insert data on the tables, it does not mean that the new number (or the highest) always on the lowest row. It randomly inserts the record. The only way you can sort it when you retrieve the rows in by using ORDER BY clause, example
SELECT *
FROM tableName
ORDER BY ID DESC
So assume that ID is numeric. If you're ID is stored as string then you should convert it to numeric,
SELECT *
FROM tableName
ORDER BY CAST(ID AS SIGNED) DESC
UPDATE 1
It should be
$res = mysql_query("SELECT * FROM data ORDER BY `id` DESC");
not
$res = mysql_query("SELECT * FROM data ORDER BY 'id' DESC");
what you have done was you have surrounded the ID with single quote forcing the server to read it as String and not Numeric
When you are not using any ORDER BY clause, the order in PHPMyAdmin is kind of random. Indeed, new rows could take place of old ones.
I am curious how you sort DESC in PHP though.
Don't absolutely trust in GUI web interface because sometimes (not always) it bugs with session management, however you can run SQL Queries using it.
1- To run a SQL query, click on "SQL" on the navigation bar.
2- Enter your SQL in the box provided.
SELECT *
FROM tableName
ORDER BY ID DESC
3- Click "Go".

Categories