mysql order by column within first column - php

is it possible to order within the first column like
select * FROM table where........ order by price desc and distance, distance
price holds values
0, 10, 24, 30 ...
and distance are values out of the lon/lat calculation. and now it displays it like this:
value1(price=30) ... 19,8km
value2(price=24) ... 8,2km
value3(price=10) ... 48km
//and then it starts with the entries in which price is 0 with the correct distance order
value4(price=0) ... 1,20km
value5(price=0) ... 1,28km
value6(price=0) ... 2,74km
and so on...
and what i want is, that within the ones where price is > 0 it shall first display them BUT with the distance as order method, then the ones with price = 0 and distance
so basically it should ignore the price value itself, just price > 0 before 0 and order by distance within the price. how could that query look like? thanks for any help :)
so this is what i want it to be;
value2(price=24) ... 8,2km
value1(price=30) ... 19,8km
value3(price=10) ... 48km
value4(price=0) ... 1,20km
value5(price=0) ... 1,28km
value6(price=0) ... 2,74km
and so on...

using order by
example
select * from price order by asc
or
select * from price order by desc
up to you

How about creating a calculated column to sort on:
SELECT *, IF(price > 0, 0, 1) AS pricegtzero FROM table WHERE........ ORDER BY pricegtzero, distance

Related

How to sort with 0 on the end in result

I have a list of products with prices. Some products are prices only aviable on request. This products are labeled with a price of 0 in the database.
If I now make a request like:
$sql = "SELECT * FROM products WHERE ORDER BY Price ASC";
The Results are of course starts with a price of 0.
But I want all the 0 prices at the end of the results.
Is this possible with a SQL request or what is the best solution to re-sort in php?
Thx in advanced
Bernd
You may sort using a CASE expression which places zero prices last:
SELECT *
FROM products
WHERE ...
ORDER BY
CASE WHEN Price > 0 THEN 0 ELSE 1 END,
Price;
In MySQL, the ORDER BY clause can be simplified:
SELECT *
FROM products
WHERE ...
ORDER BY
Price = 0, -- false (0) for positive prices, true (1) for zero prices
Price;
Yes you can by using bitwise negation to make 0 the maximum value for a bigint in the sort.
SELECT *
FROM products
ORDER BY
CASE WHEN Price = 0 THEN ~0 ELSE Price END ASC
You can mysql FIELD function to retrieve the data in the order you want
SELECT * FROM products WHERE ORDER BY FIELD(Price,0) ASC

Sort column ascending but I want 0 to come last and RANDOMLY using mysql

I need to get data from MySQL table in ascending order but zero values come last and randomly
Right now this is my order by condition.This is not working
ORDER BY sortorder=0 RAND(),sortorder
Use conditional ordering
select *
from table
order by column > 0 desc, column asc, rand()
Add rand() at the end
Demo
Or you could use union
(select * from table where column > 0 order by column asc)
union all
(select * from table where column = 0 order by rand())
Demo
If you want to get data from mysql randomly and zero values come last. You may want to try the following:
SELECT * FROM table ORDER BY `column` = 0, rand();

Write query for positive value in ascending order and negative value in

I have mysql table that looks like below :
In this table there are minus and plus value. I wants all record in dropdown in a format like.
I need to show first as zero than minus value in descending order and positive value in acsending order. So what query should I have to write?
For example :
0.00
-1
-2
...
...
...
1
2
3
...
...
...
Try this:
ORDER BY CASE
WHEN Value = 0 THEN 0
WHEN Value < 0 THEN 1
ELSE 2
END ASC,
ABS(Value) ASC
The first part of the ORDER BY places 0 value first, followed by negative values, followed by positive values. The second part orders negative values in descending order and positive values in ascending order.
Use 3 querys and join the results together by using union
(select * from sph where value = 0)
union
(select * from sph where value < 0 order by value desc)
union
(select * from sph where value > 0 order by value asc)
select * from sph order by abs( `value` ) asc
simple ;)

How to select the last n row in MySQL?

with this code I select the first 30 row of the table:
SELECT * FROM `table` LIMIT 0 , 30
But how to select the last 30, without changing the order?
It looks like everyone is missing this part:
But how to select the last 30, without changing the order?
First of all, clearly there is no order in the query provided. Assuming the order is ascending on some field this would be the query #DannyFox meant:
SELECT * FROM T
ORDER BY val
LIMIT 0 , 30
Now imagine we have simplified data, such as a, b, c, d, e and that we want only 3 rows instead of 30:
SELECT * FROM T
ORDER BY val
LIMIT 3
If this returns: a, b, c, d, e in each row, then he would expect to get c, d, e in that order. The query everyone is providing:
SELECT * FROM T
ORDER BY val desc
LIMIT 3
Would return e, d, c. The problem with this is that it's actually changing the original order, and the OP say he didn't want to change the order. So technically, the query that would result in c, d, e is:
select * from (
select * from t
order by val desc
limit 3
) s
order by val
Which actually changes the order twice, getting the original order back.
Since you are trying to avoid ordering, then the solution would be to apply it twice.
SELECT *
FROM (
SELECT *
FROM `table_name`
ORDER BY `column_name` DESC -- maybe id?
LIMIT 0, 30
) `table_aliase`
ORDER BY `column_name` ASC
First you need to specify an order:
SELECT * FROM table
ORDER BY some_id ASC -- ascending order
LIMIT 30
If that query returns the first 30 columns, this one will return the last 30:
SELECT * FROM table
ORDER BY some_id DESC -- descending order
LIMIT 30
If you have an auto incremental key/column, say id then here's an example
SELECT * FROM `table` ORDER BY id DESC LIMIT 0 , 30;
Maybe this will work: select * from table WHERE id > ((SELECT MAX(id) from table) - 30);
Nothing is said about an order, so you can not use ORDER BY.
There is a way to get records from a given point, but for that you need to know how many records there are, then use this counted value to provide the limits
SELECT COUNT(*) AS counted FROM table
SELECT * FROM table LIMIT (counted-30),30
Here's my method used with PHP:
$query = "SELECT * FROM table ORDER BY id DESC LIMIT 3";
$res = mysql_query($query);
$results = array();
while($row = mysql_fetch_assoc($res)){
$results = $row[field];
}
// Back to original order based from DB
$results = array_reverse(results);

ordering mysql table for the max of two variables

hope someone can help me with this issue.
I have a table with two columns, and i want to select items from that table and order them depending on which of these two values is higher.
lets say i have columns 'x' and 'y' and i have these entries:
x ; y
1.- 10 ; 12
2.- 5 ; 10
3.- 11 ; 20
i want the response to be ordered: 3, 1, 2
i know this wont help, since it has no sence in the mysql, but is a representation of my idea
$query = (mysql_query("SELECT * FROM productos ORDER BY MAX(x, y)"));
i dont want to create a new variable in the table for this query.
Any idea? thanks a lot
You want to sort it after x or y, depending which is higher?
SELECT * FROM products ORDER BY GREATEST(x, y)
But in your case this is 3, 2, 1 because 15(y) is higher than -5(x) and also higher than the greatest in row 1. So I am a bit confused.
Supporting NULLs and correct order:
SELECT * FROM products ORDER BY GREATEST(COALESCE(x, 0), COALESCE(y, 0)) DESC
If you need other value as default than 0 just replace it.
Select
*
from
products
order by
Case when x>y then x else y end DESC
SELECT *
FROM products
ORDER BY GREATEST(x,y) DESC
, LEAST(x,y) DESC
The second part of the ordering (LEAST) is to put a row with (12,20) before a row with (10,20).
Since the two fields can have NULL:
SELECT *
FROM products
ORDER BY GREATEST( COALESCE(x,0), COALESCE(y,0) ) DESC
, LEAST( COALESCE(x,0), COALESCE(y,0) ) DESC ;

Categories