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.
Related
I'm building chat application. In the chat screen I want to display last 20 message and if we scroll up, it will load older message. Could anyone bring me SQL query string to load next 10 last records?
This is my SQL string to load last 20 records:
SELECT *
FROM
(
SELECT *
FROM tbl_chat_content
ORDER BY message_id DESC
LIMIT 10
) AS TEMP
ORDER BY TEMP.message_id ASC
If your ID is alwais increasing, your query will return the last 10 records:
SELECT *
FROM
(
SELECT *
FROM tbl_chat_content
ORDER BY message_id DESC
LIMIT 10
) AS TEMP
ORDER BY
TEMP.message_id ASC
you can specify an OFFSET to get the next 10 records (from 11 to 20):
SELECT *
FROM
(
SELECT *
FROM tbl_chat_content
ORDER BY message_id DESC
LIMIT 10 OFFSET 10
) AS TEMP
ORDER BY
TEMP.message_id ASC
this from 21 to 30, etc.:
LIMIT 10 OFFSET 20
or different syntax:
LIMIT 20,10
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;
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
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
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