MySQL IFNULL on 4 or 5 columns - mysql

I have a statement that is:
ORDER BY IFNULL(rrp, wholesale) ASC
...but I wanted to know if there is a way to have an IFNULL statement that checks 3 or 4 or 5 columns? All from the same table.

Use COALESCE
e.g:
SELECT ....
FROM ....
ORDER BY COALESCE(rrp, wholesale, column3, column4)

You could chain them up, so the expression picks the first not-null value from a list of columns:
ORDER BY IFNULL(IFNULL(IFNULL(rrp, wholesale), some_column), some_other_column) ASC

Try using COALESCE; it may give you what you need.

Related

Mysql forcibly order by exact value

Need help with mysql request.
I make a request and I want to order result by the field ID.
ID for exemple: 1,2,3,4,5,6,7,8,9
But I need that it would be ordered like this: 6,3,1,2,4,5,7,8,9
Is there any solution in mysql to forcibly put ID=6 in 1st place, ID=3 in 2nd...?
or I want the impossible)
Use CASE expression in ORDER BY clause.
Query
select * from `your_table_name`
order by case `id` when 6 then 1 when 3 then 2 else 3 end, `id`;
Find a demo here
Use:
ORDER BY FIELD(id, 6,3,1,2,4,5,7,8,9)
The FIELD function returns the position of the first value in the remaining list.

How can I use column relative position in if statement in order by?

I run a SQL query like below in MySQL:
select *
from (
select 2 as o,1 as t from dual
union
select 1 as o,2 as t from dual
) x
order by if((select 1),o,t);
It works well, but when I use column relative position in if statement, it doesn't work.
How can I use column relative position in if in ORDER BY statement?
select *
from (
select 2 as o,1 as t from dual
union
select 1 as o,2 as t from dual
) x
order by if((select 0),1,2);
I'm not sure what your real confusion is. When an integer appears in an order by, then this is treated as a column number. Any other use of an integer is interpreted as an expression.
The use of column numbers has been removed from the SQL standard. Hence, its use in any particular database is not guaranteed in future releases. It is really better to use the column names.
I think you want to sort your query based on a criteria over two columns, if I'm correct, you can use something like this:
...
order by
case when (your criteria)
then column1
else column2
end;
Note: use union all instead union when you don't want to remove duplicate values as performance issue ;).

mysql ordering exception

I have a MySQL database and a have a funny question.
I need to order the results of a query by a field which has entries of 1,2,3 or 4, ordered descending but with 4 at the end.
So I wish to have the results in the following order
3's
2's
1's
4's
Is this possible at all?
I know I can do order the result array in php but unfortunately I need to do this in an sql statement.
If the field is an int,
ORDER BY (fieldname = 4) DESC, fieldname DESC
should do the trick.
Here is another cool way
ORDER BY MOD(fieldname,4) DESC,fieldname
If the result is a CHAR(1) then
ORDER BY LOCATE(fieldname,'3214'),fieldname
add this to the order
ORDER BY
CASE field_name WHEN 4 THEN 1
ELSE 2
END
this will return the result of the query order using the value of the field

Issue writing MySQL query to find difference

I'm having trouble with sorting out a query for the following:
Data:
Column1 Column2
2 0
0 -2
I'm trying to select the difference between Column1 and Column2, with a minimum of 0. Eg.
Row1=2
Row2=0
My current query is SELECT (Column1 - Column2) as total FROM blah.
I tried adding max(Column2, 0) into the query, but that just errors.
Try:
SELECT GREATEST(Column1 - Column2, 0)
from Table
The MySQL function MAX() is an aggregate function, often used in combination with a GROUP BY. It only takes one argument (the name of the column of which you want to select the maximum value). The GREATEST() function is the function you need (as Michael Pakhantsov pointed out).

MySQL select with subquery having replace

So I have a data with format like ;1;;2; and then I need to use this number in a query so I thought I'd convert it to 1,2 and use that in a IN condition. In my table, the result should return 2 rows but instead it is returning only 1 row.
My query is like this. The subquery return 1,2 with no problem but only 1 row is retrieve.
select *
from wt_lists
where id IN ((select replace (replace(sendto, ';;',','),';','')
from wt_stats where statsid IN (1)))
But when I try it with this. It returns the correct result, which in my case is 2 rows.
select *
from wt_lists
where id IN (1,2)
What am I missing here?
Comma delimited strings need to be explicitly defined in the query in order to be used in the IN clause - there's countless examples on SO where people need to use dynamic SQL to incorporate user submitted comma delimited strings.
That said, I have a solution using the FIND_IN_SET function:
SELECT DISTINCT wl.*
FROM WT_LISTS wl
JOIN (SELECT REPLACE(REPLACE(ws.sendto, ';;',','),';','') AS ids
FROM WT_STATS ws
WHERE ws.statsid = 1) x ON FIND_IN_SET(wl.id, x.ids) > 0
You are replacing the string:
';1;;2;'
To:
'1,2'
So, you SQL query looks like:
select * from wt_lists where id IN ('1,2') from wt_stats where statsid IN (1)
To use IN clause you need select different values in different rows.
I found this store procedure that does exactly what you need.
http://kedar.nitty-witty.com/blog/mysql-stored-procedure-split-delimited-string-into-rows/
I have not tested, but it is the way.
Obs: Like David said in the comments above, parsing the data in your application is a better way to do this.