Get random top 3 record from database : php-mysql - php

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.

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

PHP MYSQL ORDER BY DESC then ASC

I have got this MYSQL
SELECT * FROM chat WHERE To_='$NameId' OR From_='$NameId' ORDER BY `DATE` DESC LIMIT 0,50
This code returns me data in this order
1 newest
2 newest
3 newest
4 newest
5 newest
But i want data to be returned in this way
5 newest
4 newest
3 newest
2 newest
1 newest
How can i do that?
Use this :
SELECT * FROM (select * from chat where To_='$NameId' OR From_='$NameId' ORDER BY DATE DESC LIMIT 0,50) sub ORDER BY DATE ASC
As I understand you want to change direction of records after getting first 50 records.
You can use sub-query feature of MySQL. Assume that result of your query is new, shiny table. So write SQL for desired result using this table (actually it is data set). (In our case we rename it as results)
SELECT * FROM (select * from chat where To_='$NameId' OR From_='$NameId' ORDER BY DATE DESC LIMIT 0,50) as results ORDER BY results.DATE ASC
Also if you have "count" of results you can do same thing using single-level query. Think about that;
SELECT * FROM chat WHERE To_='$NameId' OR From_='$NameId' ORDER BY `DATE` ASC LIMIT ***MyCount***,50
MySQL will not force you to rename your sub-query results but renaming
them results in more readable SQL codes.

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

Elimnate the 5 newest records from consideration

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

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

Categories