Elimnate the 5 newest records from consideration - php

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.

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"

Try
SELECT *
FROM `tbl_Name`
ORDER BY `Hits_Field`
LIMIT 5,5
LIMIT actually can have two parameters: an offset and the amount of records. So if you want to drop the first 5 records and the select the next 5, use LIMIT 5,5.

You can offset
SELECT * FROM tbl_Name
ORDER BY Hits_Field LIMIT 5,5

Use Limit parameters.
1) To eliminate Newest 5 records
SELECT * FROM tbl_Name ORDER BY Hits_Field ASC LIMIT 5 [Parameter to display from **5** th record], 1000[Parameter to display up to 1000 record];
2) To eliminate Least-viewed(Oldest) 5 records
SELECT * FROM tbl_Name ORDER BY Hits_Field DESC LIMIT 5 [Parameter to display from **5** th record], 1000[Parameter to display up to 1000 record];
Hope This Will Help.

The logic would be
Grab the 10 least viewed (to have enough records to eliminate 5),
remove any that also show up in the newest (up to 5, but could be less),
then limit that quantity to 5
Try EXCEPT or MINUS, something like
SELECT * FROM
(
SELECT * FROM tbl_Name WHERE ORDER BY Hits_Field LIMIT 10
EXCEPT
SELECT TOP 5 * ORDER BY ID_Record DESC
)
LIMIT 5

Related

Can I SELECT from mysql 5 values from top N(10)? (rand)

I need take from MYSQL 5 values that taked from top 10 values randomly?
Exmple:
I'v 100 posts in my db and I want take 5 of them that have views number in top 10 posts. How can I do it by only one command in mysql?
SELECT * FROM
(SELECT * FROM posts order by id DESC limit 0,10) as p1
ORDER BY rand() limit 0,5
Please change fields according your database structure

Select from last ID and older than it by 25 [MySQL]

I have posts which has fields (ID,title,date) What I'm looking for it to select rows order by date desc just limit for 25 records
lets say if we run that one it will show the last row with ID (600) like this:
(600,601,602,.....,625)
so I want after that to select another 25 records but min(id) before the last one (600) so it will be like this
(575,576,577,.....,599)
For example consider that the first result gives the descending last five IDs.
SELECT idprocess FROM process ORDER BY idprocess DESC LIMIT 5
248034
248033
248032
248031
248030
You could use a SELECT into a temporary table to mess with your result set's ORDER BY and LIMIT. The sub-query LIMIT should be the sum of the records you want back and how many you want skipped off the end.
SELECT t1.idprocess FROM
(SELECT idprocess FROM process ORDER BY idprocess DESC LIMIT 10) AS t1
ORDER BY idprocess ASC LIMIT 5
248025
248026
248027
248028
248029

Get random top 3 record from database : php-mysql

I want to create a random image banner which fetches the image data from a MySQL database.
And I only want to fetch the latest three records, randomly.
How can I fetch the the 3 most recent records, in a random order?
I am the following query:
SELECT * FROM bottom_advt WHERE bottom_advt_page_name='News' ORDER BY RAND() LIMIT 1
but it's not working.
You said 3 records. So try:
SELECT * FROM bottom_advt WHERE bottom_advt_page_name='News' ORDER BY RAND() LIMIT 3
You're limiting your results to one with LIMIT 1. Change it to LIMIT 3 to get three results.
Try this if you want one of the last three records:
SELECT * FROM bottom_advt WHERE bottom_advt_page_name='News' AND id+3>last_insert_id() ORDER BY RAND() LIMIT 1
You could use this:
SELECT *
FROM bottom_advt
WHERE ID IN (SELECT * FROM (
SELECT id
FROM bottom_advt
WHERE bottom_advt_page_name='News'
ORDER BY id DESC
LIMIT 3) last)
ORDER BY RAND()
LIMIT 1
Subquery will return the last 3 id that have bottom_advt_page_name='News', and the outer query will randomly select one of those.
Please see fiddle here.

Php MySQL Most Liked Query for last 100 Records

I am using this query:
SELECT * from likes GROUP BY url ORDER BY count(*) DESC LIMIT 6
to fetch most liked record from my table 'likes'. It is working perfect for fetching most liked content of all time.
But now to I want to select the 6 most liked record from the last 100 records.
What will be the query for it ?
SELECT * FROM (select * from likes order by date desc limit 100) xx
Group by URL order by count(*) limit 6
Obtain the primary keys of the last 100 entries and narrow your query to it. Probably extremely easy if you have auto-increment keys.
SELECT * from likes
GROUP BY url
ORDER BY count(*) DESC
WHERE ID > MAX(ID)-100
LIMIT 6

"Order by desc" in reverse order?

Welcome,
I'm wondering is it possible to reverse returned data in sorting "order by desc" but i want that data in reverse order.
For example, i got table with values
ID
1
2
3
4
And i do
Order by ID ASC LIMIT 3
I got
1
2
3
When i do Order by ID DESC limit 3
i get
4
3
2
I would like to have
3
2
1
So i would like to order by ASC but revers results.
I was always doing this in PHP side using array_reverse, but today i want ask You.
Maybye i'm wrong and i can do this just in Mysql.
Regards
SELECT *
FROM (
SELECT ...
FROM ...
ORDER BY ID ASC
LIMIT 3
) AS sq
ORDER BY ID DESC
Think of it as working in two steps. First it executes the inner query: selects 3 records with lowest IDs. Then in the outer query it sorts them in descending order.
You can fetch the first three rows using a subquery and then reverse the order of these rows in an outer query:
SELECT *
FROM
(
SELECT *
FROM yourtable
ORDER BY ID
LIMIT 3
) T1
ORDER BY ID DESC
Nope, you aren't wrong. There are no difference for any sensible amount of data. Array_reverse is all right.
That's not a thing you have to be concerned too much of. Just use whatever you like more - for readability or other subjective reasons
You can do this with a sub query :
SELECT * FROM
(SELECT * FROM myTable ORDER BY idMyTable LIMIT 0, 3) AS r
ORDER BY r.idMyTable DESC
Resources :
Mysql - Subqueries
You could use an outer SELECT to reverse the order:
SELECT *
FROM (
SELECT …
ORDER BY id ASC
LIMIT 3
) sub
ORDER BY id DESC

Categories