how to have 2 oder by in one sql query - mysql

I have a situation where I want to order a column from the highest value to the lowest, retrieve the highest 10 value, then after that order by random the top 10 value to display. May I know how to write a query on this situation?

SELECT *
FROM (SELECT *
FROM table
ORDER BY VALUE DESC
LIMIT 10) AS mytable
ORDER BY Rand()
Gives you a random ordering of the top 10 results

Related

How can I select the row from a table whose 1 field is maximum or minimum?

In my table I have a field name rt_stars which includes integer anyone between 1 to 5. Suppose there are 5 rows having rt_stars 4,4,3,2,1 respectively. Here, 4 is the highest and 1 is the lowest. If I want to select the row with the maximum value 4 how can I do that? Since there are two 4 here the last one will be selected. Can I have query something like this?
SELECT * FROM ratings WHERE MAX(rt_stars) ORDER BY rt_id DESC LIMIT 1
I know this is wrong but that's how I want to select all the values from the rows if the rt_stars field has the maximum value in it. How can I achieve this kind of a query?
You can select one row using:
select r.*
from ratings r
order by rt_stars desc, rt_id desc
limit 1;
The problem with your query is that you cannot use max() in the where clause. Beyond that, you don't need aggregation at all -- just ordering the rows and then selecting the first one.
If you want all rows with the maximum stars you can do:
select *
from ratings
where rt_stars = (select max(rt_stars) from ratings)
If you just want one of them randomly you can do:
select *
from ratings
order by rt_stars desc
limit 1
I think the below SQL code will help.
SELECT * FROM ratings WHERE rt_stars = MAX(rt_stars) ORDER BY rt_id DESC;
Sorry the code modified below
SELECT * FROM ratings WHERE rt_stars = ( SELECT MAX( user_05_pk ) FROM ratings )
ORDER BY rt_id DESC;

MySQL - Order by one column for a limit then by another column for a limit

I'm developing an application that displays rows which have columns "Time created" and "Number of Likes".
I'm trying to return the first 5 results of my query with the rows that have the most likes, and then the remaining 95 by most recent date, without duplication.
My current query only accomplishes ordering by date.
"SELECT * FROM `$category` ORDER BY time DESC LIMIT 100"
Is what I'm attempting to do possible in one query? Or will I have to perform two queries and filter out duplicate rows?
UNION automatically removes duplicate rows and LIMIT outside the parethesis applies to the whole query.
(SELECT *, like_count AS likes FROM category ORDER BY like_count DESC LIMIT 5)
UNION
(SELECT *, 0 AS likes FROM category ORDER BY time DESC)
ORDER BY likes DESC, time DESC
LIMIT 100

How do I select the 1st row from a selection of 10 random rows

Lets say I have a 1000 rows in my table.
I want to select 10 of those at random.
SELECT * FROM table ORDER BY RAND() LIMIT 10
Then I want to select the row in that result with the highest value for number
SELECT * FROM table ORDER BY number DESC LIMIT 1
Can anyone help me come up with an efficient way of doing this?
Just use a subquery:
SELECT *
FROM (
SELECT * FROM table ORDER BY RAND() LIMIT 10
)
ORDER BY number DESC LIMIT 1

why isn't this returning random stuff?

EDIT AGAIN: someone posted the solution, but it has a wierd x I don't understand why the x is there but now it works!! SOMEBODY EXPLAIN !!!
EDIT: I NEED THE LAST 12 RECORDS IN TIME, I USE ID INSTEAD OF TIME BECAUSE THE LATER ONES IN TIME ALSO HAVE A BIGGER ID, I GUESS THAT DOESN"T WORK BECAUSE OF THE PRIMARY KEY THING??
here is a mysql statement
"SELECT * FROM items WHERE item_section='$section'
ORDER BY item_id DESC, rand() LIMIT 12"
I need to add the DESC because I want the most recently posted item corresponding to a larger ID number. What I want is the last 12 records, but for those 12 to be in random order. This isn't working, what gives? I think I am just getting the latest 12 records NON-randomized.
If you turn what you said into sql, you get the answer:
SELECT * FROM (
SELECT * FROM items
WHERE item_section = '$section'
ORDER BY item_id DESC
LIMIT 12) x
ORDER BY rand()
You first select the most recent 12 records (aliased as x here), then you order them randomly... that takes two queries: one nested inside the other.
Because your item_id is probably a primary key and thus the order is fully determined and rand() is a noop.
EDIT: since you probably don't care about the answer "why", what you want to do is remove item_id DESC, part:
SELECT * FROM items WHERE item_section='$section'
ORDER BY rand() LIMIT 12
and if you want that sorted by id
SELECT * FROM (SELECT * FROM items WHERE item_section='$section'
ORDER BY rand() LIMIT 12) AS q ORDER BY item_id;
and if you want randomized first 12
SELECT * FROM (SELECT * FROM items WHERE item_section='$section'
ORDER BY item_id DESC LIMIT 12) AS q ORDER BY rand();

MySQL limit from descending order

Is it available to write a query to use same "LIMIT (from), (count)", but get result in backwards?
In example if I have 8 rows in the table and I want to get 5 rows in two steps I would:
first step query:
select * from table limit 0, 5
first step result:
first 5 rows;
second step query:
select * from table limit 5, 5
second step result:
last 3 rows;
But I want to get it vice versa. I mean from the first step I want last 3 rows and from the second I want 5 first rows. Thank you for your answer
No, you shouldn't do this. Without an ORDER BY clause you shouldn't rely on the order of the results being the same from query to query. It might work nicely during testing but the order is indeterminate and could break later. Use an order by.
SELECT * FROM table1 ORDER BY id LIMIT 5
By the way, another way of getting the last 3 rows is to reverse the order and select the first three rows:
SELECT * FROM table1 ORDER BY id DESC LIMIT 3
This will always work even if the number of rows in the result set isn't always 8.
Let's say we have a table with a column time and you want the last 5 entries, but you want them returned to you in asc order, not desc, this is how you do it:
select * from ( select * from `table` order by `time` desc limit 5 ) t order by `time` asc
yes, you can swap these 2 queries
select * from table limit 5, 5
select * from table limit 0, 5
This way is comparatively more easy
SELECT doc_id,serial_number,status FROM date_time ORDER BY date_time DESC LIMIT 0,1;