Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elimnate the 5 newest records from consideration

Tags:

sql

php

mysql

I am working on a featured page that lists records in different categories. 5 Newest, 5 Least Viewed, 5 Most Viewed..

That part is not difficult:

Newest: SELECT TOP 5 * ORDER BY ID_Record DESC

Least: SELECT * FROM tbl_Name WHERE ORDER BY Hits_Field LIMIT 5

Most: SELECT * FROM tbl_Name WHERE ORDER BY Hits_Field DESC LIMIT 5

Here is my question.. Because the newest records are possibly the least viewed they could feasibly show up in both queries. I want to eliminate the 5 newest records from consideration. How do I write a SQL Statement like this:

SELECT * FROM tbl_Name 
WHERE (NOT THE 5 NEWEST ID_Record BUT ALL OTHERS STILL IN CONSIDERATION) 
ORDER BY Hits_Field LIMIT 5

I know there is a NOT IN consideration, but I am new to this and need help writing a nested statement for this.

like image 671
Korey Inglin Avatar asked Dec 13 '25 05:12

Korey Inglin


1 Answers

May be this could work:

"SELECT * from table_name where Id_Record not in (SELECT Id_Record from table_name order by Hits_Field LIMIT 5) order by Hits_Field LIMIT` 5"
like image 84
Ishwor Avatar answered Dec 14 '25 18:12

Ishwor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!