MySQL Query order by if null - mysql

I am trying to build a MySQL query that has an order by statement. This is what I am trying to do:
SELECT *
FROM tbl_product
ORDER BY retail_price ONLY IF wholesale_price IS NULL OTHERWISE ORDER BY wholesale_price.
I have no idea where to start. I found an article that uses ORDER BY COALESCE but I also found that this could have performance issues.
Any advice is appreciated.

SELECT *
FROM tbl_product
ORDER BY ifnull(wholesale_price, retail_price);
Note that you don't need to select the value you're ordering by - it can be any expression

Related

PhpMyadmin added additions parameter in Select Query

I have tried to run a select query in phpmyadmin Sql tab as below
Select * from followers
I just run above query alone
But it return following Error in the image, can anybody help to fix this issue? can you tell me in what case it will happends?
You should use comma for adding others column in order by
Select * from followers
order by follwer_id DESC, leader_id
limit 30
or if you want filter the query you should use where
Select * from followers
where leader_id =1
order by follwer_id
limit 30

MySQL ONLY_FULL_GROUP_BY enabled with CONCAT

I have a table in a database mysql (5.7.21) like this:
+----------+--------------+-----------+-----------+
| id_price | id_reference | price_usd | unix_time |
+----------+--------------+-----------+-----------+
And I need to extract the average price (price_usd) grouped by week of year, or month (unix_time).
I prepare this query:
SELECT CONCAT(WEEKOFYEAR(FROM_UNIXTIME(unix_time)),
'-',
YEAR(FROM_UNIXTIME(unix_time))) as date,
AVG(price_usd) AS "model"
FROM price_avg
INNER JOIN reference ON reference.id_reference=price_avg.id_reference
WHERE price_avg.id_reference=1
GROUP BY WEEKOFYEAR(FROM_UNIXTIME(unix_time)),
YEAR(FROM_UNIXTIME(unix_time)),
price_avg.id_reference
ORDER BY unix_time ASC
The inner join is useful to get the name of the product having the the id.
I get this error:
#1055 - Expression #1 of SELECT list is not in GROUP BY clause and
contains nonaggregated column 'name_of_db.price_avg.unix_time' which is not
functionally dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by
I cannot change the settings of MySQL (I can't disable ONLY_FULL_GROUP_BY mode or anything else).
How do I have to change the query to extract the data in MySQL 5.7.21?
Thanks in advance.
You can use sub query so that it will be a full group by.
Select `date`,
AVG(price_usd) AS "model"
From (
SELECT CONCAT(WEEKOFYEAR(FROM_UNIXTIME(unix_time)),
'-',
YEAR(FROM_UNIXTIME(unix_time))) as `date`,
price_usd
FROM price_avg
INNER JOIN reference ON reference.id_reference=price_avg.id_reference
WHERE price_avg.id_reference=1
) t
GROUP BY `date`
ORDER BY substring(`date`, -4), substring(`date`, 1, 2) ASC;
Result:
date model
48-1998 11.99
36-2001 19.99
I solved with this query:
SELECT DATE_FORMAT(FROM_UNIXTIME(unix_time),'%Y-%m') as date, AVG(price_usd) AS model FROM price_avg INNER JOIN reference ON reference.id_reference=price_avg.id_reference WHERE price_avg.id_reference=1
GROUP BY date, price_avg.id_reference ORDER BY date ASC
I have week and year inverted but I can resolve in client enviroment!
Thank you all.
Hit this same problem, reported it as a possible bug on MySQL:
https://bugs.mysql.com/bug.php?id=90792&thanks=4
From the initial response, it sounded like it might be treated as a bug and fixed, but I think the follow-up suggests that GROUP BY expressions (instead of columns) aren't part of the SQL standard, and that determining if a complex expression is completely derived from GROUPed expressions is difficult and is, for now at least, something they decided not to pursue:
https://mysqlserverteam.com/when-only_full_group_by-wont-see-the-query-is-deterministic/
There are some workarounds in the meantime.

Expression in mysql order by not being evaluated

This problem began when I was trying to set the order by column from a bound statement.
This is not working for me:
select * from testTable order by field(?, 'userid','name') asc;
The field() function is not being evaluated for some reason.
After further investigation I found that this also does not work:
select * from testTable order by (1+1) asc
Are these both the same problem? And what is the solution?
I'm just guessing, but I think it is plausible that you can't use ORDER BY #ColumnIndex (For example ORDER BY 2) when you are using SELECT *.
Try specifying all the columns you are willing to select, or use column names in the order by clause.

MySQL INSERT... SELECT column count and virtual/aliased columns

I'm trying to insert using a select statement. However, I need to order the sub-select results using a ranking equation. If I create an alias, it throws off the column count. Can I somehow order my results using an equation?
INSERT INTO draft
( fk_contrib_id , end_time )
SELECT pk_contrib_id, UNIX_TIMESTAMP(), (X+Y+Z) AS ranking
FROM contrib
ORDER BY ranking DESC
LIMIT 1
I need the 'ranking' column for sorting, but if I do, the column count is off for the insert. Do I have to use two queries for this?
You could simply change your query to directly use the expression in the ORDER BY clause, like so:
INSERT INTO draft
( fk_contrib_id , end_time )
SELECT pk_contrib_id, UNIX_TIMESTAMP()
FROM contrib
ORDER BY (X+Y+Z) DESC
LIMIT 1
Remove the expression from the SELECT list. And use the expression in the ORDER BY clause.
ORDER BY X+Y+Z
It's perfectly valid to ORDER BY expressions that are not in the SELECT list.

Syntax error (operator missing) in query-expression?

I'm trying to understand COUNT(*), and therefore I created a testing query:
SELECT COUNT(*)
WHERE COUNT(UITLENINGEN.LLNR) >= 30;
When I click Execute, I get the following error:
Syntax error (operator missing) in query-expression COUNT(*) WHERE COUNT(UITLENINGEN.LLNR) >= 30.
What am I doing wrong?
Try this
SELECT COUNT(*) FROM UITLENINGEN GROUP BY LLNR HAVING COUNT(UITLENINGEN.LLNR) >= 30;
I don't understand what you're trying to do. The query below is based on a table which includes a field named category_id. And it uses GROUP BY category_id to count the number of rows within each such group. The HAVING clause limits the result set to only those groups whose count is at least 30.
SELECT category_id, COUNT(*)
FROM YourTable
GROUP BY category_id
HAVING COUNT(*) >= 30;
If that is nothing like what you're trying to accomplish, please give us more detailed information so we may better understand your situation. A brief set of sample data, and the output you want based on that sample, would help tremendously.
You have not specified the table from which the data should be retrieved. Try the following
SELECT COUNT(*) from tableName
WHERE COUNT(UITLENINGEN.LLNR) >= 30;
Add your table name to the query.
SELECT COUNT(*) FROM UITLENINGEN WHERE COUNT(UITLENINGEN.LLNR) >= 30;
Please, add table name and use having statement where aggregation funcion required. E.g.:
select count(*)
from UITLENINGEN
having count(UITLENINGEN.LLNR) >= 30;