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?
Related
I am trying to retrieve random rows from mysql using RAND() function. But in result rows are repeating more than once. Is there any way to return random rows in such a way that a row is returned only once?
My query is: SELECT h.recid recid, h.name name, h.subtitle subtitle, h.pricingfrom pricingfrom FROM holydays h ORDER BY RAND()
I found this solution but I didnt understand how it works as I don't have any limits...
Any answer will be appreciated.!
The ORDER BY clause of a query has nothing to with the result set, except maybe if LIMIT is being used, which it is not here. It sounds to me like your table has some duplicate records. If you want a random ordering with no duplicates, then select with DISTINCT:
SELECT DISTINCT
recid,
name,
subtitle,
pricingfrom
FROM holydays
ORDER BY RAND();
it's work by order by taking random columns
ex, there is id, name, emai then some time it order id,name,email or some time name,id,emai etc. etc. and it some time order in asc and some time in desc. That's logic.
I have a query like this,
select * from my_table where `status`='1' ORDER BY FIELD(city_id,548) ASC;
My purpose is to show all records from the table, but records with city_id=548 should come first.
Currently its showing all records but no desired sorting! Any ideas?
Actually it is working but you need to change it to DESC because FIELD() returns the index of the value in the parameter.
Other than FIELD() which accepts multiple parameters, you can alternatively use = if you only have one condition.
ORDER BY (city_id = 548) DESC
when city_id = 548 is true, it returns 1 otherwise 0 that is why we used DESC.
Try like this
descending order in sql is denoted by DESC keyword I think other values are smaller than 548 that's why it's not work as you expect
select * from my_table where `status`='1' ORDER BY FIELD(city_id,548) DESC;
I am looking to create an order based on multiple columns in my table - all date related. For example: my columns are: fee_due, fee_2_due, fee_3_due - they all contains results of various dates. However, I want to create an order combining all columns, so fee_due might be 2012-11-03, fee_2_due might be 2012-11-01 and fee_3_due might be 2012-11-02.
My query needs to be:
SELECT *
FROM table_name
ORDER BY [date] DESC
... whereby the dates from the 3 columns join to form one order, regardless of what column they are in.
Hope that makes sense and thanks in advance.
Additionally you can:
SELECT *
FROM table_name
ORDER BY fee_due ASC,fee_2_due DESC,fee_3_due DESC
You can sort each column independently according to your need.
I used the following to make it work:
SELECT * FROM table ORDER BY camp1 ASC, camp2 ASC, camp3 DESC
Have you try this:
SELECT *
FROM table_name
ORDER BY fee_due,fee_2_due,fee_3_due DESC
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
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"