MYSQL Select random from two rows - mysql

Can someone help me with this query.
I have this table for sample:
FILE Table
UID
file
uploaded_on_date
view_count
What I want to consider is the top 30 most recent uploaded file and top 30 most viewed file then randomly select from them and limit by 10.
I am new to this mysql complex queries. A sample query would be good and i will be able to understand it.
Thanks.

SELECT
*
FROM
(
SELECT
*
FROM
tablefile
ORDER BY
uploaded_on_date DESC
LIMIT 30
UNION SELECT
*
FROM
tablefile
ORDER BY
view_count DESC
LIMIT 30
)
ORDER BY
RAND()
LIMIT 10;

select * from (
select * from table order by upload_on_date desc limit 30
union
select * from table order by view_count desc limit 30) t
order by rand() limit 10

Related

Double DESC with Double LIMIT

I need a mysql SELECT which it select the last 20 ids and it shows the 7 with the biggest clicks but from the last 20 ids the 7 biggest clicks..and not from all ids I tried with DESC and LIMIT but its not working..any idea??
SELECT * FROM search ORDER BY clicks DESC, id DESC LIMIT 20
Or
SELECT * FROM search ORDER BY id DESC LIMIT 20
SELECT * FROM search ORDER BY clicks DESC LIMIT 7
Or
SELECT * FROM search ORDER BY id DESC LIMIT 20 UNION SELECT * FROM search ORDER BY clicks DESC LIMIT 7
Try this
SELECT * FROM
(
SELECT * FROM search ORDER BY id DESC LIMIT 20
) SUB
ORDER BY clicks DESC LIMIT 7
EDIT:
What is SUB doing..?
SUB is the alias for the subquery SELECT * FROM search ORDER BY id DESC LIMIT 20. It could have different names which is up to you.
SELECT * FROM /*<=== Query 2 - Will be executed after Query 1 is run, thus will perform a search within the results returned by Query1*/
(
SELECT * FROM search ORDER BY id DESC LIMIT 20 /*<=== SUBQUERY - Query 1 - Will be executed first */
)
ORDER BY clicks DESC LIMIT 7
What we're going here is that; first we get a result set of most recent 20 rows from search with SELECT * FROM search ORDER BY id DESC LIMIT 20 SUB and then we run a SELECT query IN the result set returned by SUB.
Try this:
your query should be
SELECT * FROM search ORDER BY clicks DESC LIMIT 0, 20
or
SELECT * FROM search ORDER BY id DESC LIMIT 0, 20
Where ' 0 ' represent the starting position of your result and ' 20 ' represent the length of your result.

how to select some record by random between the some records?

I have a table of images with the some column like this: id, path, name, likeCount
I want to have a query that select an image by random between those 20 images with the highest likeCount .
Do you have any idea?
In that case, you need to do that in 2 steps:
First, you need to create a view in which you store the 20 first images with the highest likeCount:
CREATE VIEW first_Twenty_images AS
SELECT *
FROM images_table
ORDER BY likeCount DESC
LIMIT 20
After that, you can select a random element from them by using the view we created just before:
SELECT * FROM first_Twenty_images ORDER BY RAND()
Try like this Not Tested.
Select * from ( select #a:=#a+1 no, id, path, name, likeCount from
table1,(SELECT #a:= 0) AS a order by likeCount desc limit 20 ) as tt order by
Floor(RAND() * 20) limit 1
you can user rand() in query
SELECT * FROM table_name ORDER BY RAND()

How do I select 5 random rows from the 20 most recent rows?

I want to select 5 random rows from a table but only from the 20 most recent rows. I know the 2 statements separately would be something like:
SELECT * FROM table ORDER BY RAND() LIMIT 5
SELECT * FROM table ORDER BY date DESC LIMIT 20
How would I combine these 2 statements so it will select 5 random rows from the 20 most recent rows? Thanks.
Use an nested select
SELECT foo.* FROM (SELECT * FROM table ORDER BY date DESC LIMIT 20 ) as foo
ORDER BY RAND() LIMIT 5
Simply nest them:
SELECT * FROM (
SELECT * FROM table ORDER BY date DESC LIMIT 20
) ORDER BY RAND() LIMIT 5
Look up subqueries!
SELECT d.* FROM (SELECT * FROM table ORDER BY date DESC LIMIT 20) as d ORDER BY RAND() LIMIT 5;

How to select from a selection?

I'm creating a commenting system, which will have 2 top comments.
How can I select the latest 20 rows, and then from that selection, select the top 2 rows (likes-dislikes)? I can do it with a PHP loop, but it would not be as efficient. Currently I am just selecting the top 2 from the all the comments, but the two top comments never change, since people just up-vote those ones:
SELECT * FROM pagecomments WHERE page_id='$pageid' ORDER BY likes-dislikes DESC LIMIT 2
EDIT: The table is ordered by the the column "id", which is auto_increment. page_id is the page on the site. Sorry.
Nest your query:
SELECT *
FROM (
SELECT *
FROM pagecomments
WHERE page_id='$pageid'
ORDER BY date DESC
LIMIT 20
) t
ORDER BY likes-dislikes DESC
LIMIT 2
Order not only by LIkes, first order by date entered or timestamp. By ordering by date, you assure that you`ll get the latest 20 posts or comments.
SELECT * FROM pagecomments WHERE page_id='$pageid' ORDER by date_entered desc ,
likes-dislikes DESC limit 2
Since your id column is set to auto_increment, use it in a subquery:
select *, likes-dislikes
from (
select *
from pagecomments
where page_id='$pageid'
order by id desc
limit 20
) t
order by likes-dislikes desc
limit 2
Condensed SQL Fiddle Demo

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