Select top 10 rows from database and display result randomly - mysql

I want to display trending Ads on my site on the basis of number of clicks.
My SQL Query looks like this:
SELECT ad_id, clicks from ads ORDER BY clicks DESC LIMIT 5
Result:
ad_id clicks
3393 2204
4495 1208
2399 932
2780 777
3316 679
I want to display this result randomly every time page refresh.
I tried using
SELECT * from ads ORDER BY clicks DESC, RAND() LIMIT 10
But this is not working giving the same result every time on page refresh.
If I use like:
SELECT ad_id, clicks from ads ORDER BY RAND(), clicks DESC LIMIT 10
ad_id clicks
9762 0
6305 1
4040 17
11598 0
11347 0
It is showing data randomly but now the highest number of clicks is zero.
Can you suggest me how to display top clicks result randomly on every page refresh.

If you want to get top 10 rows and then display them in random order, use this:
select *
from (
select *
from ads
order by clicks desc LIMIT 10
) t
order by rand() desc
If you want to get 10 randomly chosen records sorted in descending order of clicks, try this:
select *
from (
select *
from ads
order by RAND() LIMIT 10
) t
order by clicks desc
It finds 10 random records in subquery and then sorts it afterwards.
Or perhaps you want to get 10 random records out of some top , say 100, rows sorted in descending order of clicks:
select *
from (
select *
from (
select *
from ads
order by click desc LIMIT 100 -- change this as per your needs
) t
order by rand() limit 10
) t
order by clicks desc

Related

SQL query for top 100, displayed in reverse

I have a database of logos, 18K strong. Each logo has a score. I want to select the top 100 by score and display them from 100 to 1, not from 1 to 100.
My query is:
SELECT * FROM tbllogos WHERE status = 'live' ORDER BY score DESC LIMIT 100
which works fine for selecting the top 100, but the php WHILE loop then displays them from 1 to 100. I can't figure out how to swap the order so it displays 100 to 1. Changing DESC to ASC obviously isn't the answer as that selects the 100 with the lowest scores.
Use a subquery:
SELECT t.*
FROM (SELECT *
FROM tbllogos
WHERE status = 'live'
ORDER BY score DESC
LIMIT 100
) t
ORDER BY score ASC;

MYSQL LIMIT only the values where "viewed"=1 , but not viewed="0"

This is a tough one for me. I have a table that holds user notifications. There is a column "VIEWED" where 0 means it has NOT been viewed, 1 means it has.
I want to generate my list of notifications, SELECT * that is unviewed (0), however in the case that I have no un viewed notification, I don't want to just display "no notififications" I want to display a few old ones.
I'm already showing my most recent unviewed first. Is there a way to do something that shows ALL unviewed, but only a few viewed? Like below?
$QUERY= "SELECT * FROM point_member_notifications WHERE account_id='$accountid' ORDER BY viewed ASC, created_date DESC (LIMIT 10 WHERE VIEWED='1') "
Maybe you want UNION ALL, to limit the 'VIEWED' old ones, just place the limit clause inside the parentheses that enclose the SELECT
(SELECT * FROM point_member_notifications WHERE account_id='$accountid' ORDER BY viewed ASC)
UNION ALL
(SELECT * FROM point_member_notifications WHERE account_id='$accountid' WHERE VIEWED='1' created_date DESC LIMIT 10);
You could use a combination of LIMIT and UNION (or UNION ALL if you don't want to remove duplicate records). That could look like:
(SELECT *
FROM point_member_notifications WHERE account_id='$accountid' AND VIEWED='1'
ORDER BY created_date DESC LIMIT 10)
UNION ALL
SELECT *
FROM point_member_notifications WHERE account_id='$accountid' AND VIEWED='0'
ORDER BY created_date DESC
LIMIT 10
If you want to have 10 records unviewed + the 10 old records you need to put the 2nd SELECT in brackets too, otherwise the LIMIT 10 at the end will limit the whole thing. An ORDER BY viewed should not be necessary since UNION already selects the unviewed first and then the others.

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 get data from mysql db from nth row to nth row?

I am using this query but it is not working in mysql
SELECT TOP 1 * FROM (SELECT TOP 5 * FROM ads ORDER BY id DESC) ads ORDER BY id DESC
You can use ORDER BY ... LIMIT {[offset,] row_count | row_count OFFSET offset} (lets say you want to get 5 records from 10th - 10,11,12,13,14):
SELECT * FROM ads
ORDER BY id DESC
LIMIT 10,5
Although I assume you don't want to get them sorted by id but rather by views or similar criteria where ORDER BY views DESC would take a place (don't forget to to add index on views count).
The 'TOP' does not function on MySQL. You can edit the query in the following manner to get the job done
SELECT * FROM (SELECT * FROM ads ORDER BY id DESC LIMIT 5) ads ORDER BY id DESC LIMIT 5

LIMIT then RAND rather than RAND then LIMIT

I'm using full text search to pull rows.
I order the rows based on score (ORDER BY SCORE) , then of the top 20 rows (LIMIT 20), I want to rand (RAND) the result set.
So for any specific search term, I want to randomly show 5 of the top 20 results.
My workaround is code based- where I put the top 20 into an array then randomly select 5.
Is there sql way to do this?
You can do this using an inner select. Select the top twenty rows in the inner select. In the outer select order these rows randomly and select the top five:
SELECT *
FROM (
SELECT *
FROM table1
ORDER BY score DESC
LIMIT 20
) AS T1
ORDER BY RAND()
LIMIT 5