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.
Related
I am trying to use select in combination with an arithmetic expression
SELECT * FROM `mytable` ORDER BY (`column1` / max(`column1`)* `column2`);
The problem is that it return a single row rather than all the rows sorted base on the expression.
Any idea?
Just order by column1. Dividing by a constant doesn't change the order.
EDIT: Add your expression as another field in the select. E.g.,
SELECT *, x+y as z ORDER BY z
See https://stackoverflow.com/a/10762333/2877364 for a full example of a similar situation.
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.
I have data TSA, TSB, TSC, Total.
How to display this alphabetically with Total always the last one.
Currently I have this and of course it doesn't work.
select * from table where main_id =x group by col-name asc
Certainly I can't use desc because I have another record ABC, BCA, CDA, Total.
So how to add "except if col-name is Total"? or perhaps there is another way?
Kind of hard to be sure with your post (not a lot of detail in there). But you could probably use a case statement to evaluate your column, and sort on that. Something like
case when <your column> = 'Total' then 'ZZZ' else <your column> end as SortKey
Then you can just order by that new column.
User order by field:
select * from table
where main_id =x
order by FIELD( `col-name`, 'Total' ), t;
See:
SQL Fiddle Example
Refer to:
MySQL: FIELD(str,str1,str2,str3,...)
Return the index (position) of the first argument in the subsequent
arguments.
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
I have a mysql query, something like this:
SELECT users*100 as totalusers, totalusers*90 AS totalsalerys FROM table
As you can see I want to reuse the totalusers when calculating totalsalerys so I son't have to write the same code again. That dosen't seem to work in mysql, is there another easy way to do this?
My query is just an example, change the *100 and *90 to some very long formula and you might see what i mean..
SELECT (#r := users * 100) as totalusers,
#r * 90 AS totalsalerys
FROM table
You can also use a subquery as #Tom Ritter advices, but it's not friendly to ORDER BY ... LIMIT ... clause.
In MySQL, all results of the inner subquery will be fetched before applying any filters in the outer subquery.
I believe you would have to copy/paste the formula or use a subquery. The below would work in T-SQL, but I imagine it'd work in MySQL as well since it's pretty simple.
SELECT
x.totalusers, x.totalusers*90 AS totalsalerys
FROM (
SELECT users*100 as totalusers FROM table
) x