I have a table called 'topics' in which all topics are saved. I want to select the latest 5 rows from that table, show them on one page, then select THE OTHER FIVE latest ones and show them on the other page.
I know how to echo all the topic names in a while loop, but the problem here is making mysql select 5 rows, then the other five for another page, not the same ones again. How to achieve this?
SELECT
*
FROM tablename
ORDER BY id DESC
LIMIT 0, 5
on the another page:
LIMIT 5, 5
$start = 0;
$count= 5;
$query = "select *
from topics
order by date desc
limit $start, $count"
First 5:
SELECT * FROM your_table ORDER BY any_order_criteria LIMIT 0,5
Second 5:
SELECT * FROM your_table ORDER BY any_order_criteria LIMIT 5,5
What i got from your problem is that you want to enable Pagination on your page.
This is a wonderful article on Pagination using PHP and MySQL.
http://www.tonymarston.net/php-mysql/pagination.html
Related
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
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.
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
I have a mysql table named events. Some events are featured. I want to randomly display one of the two latest featured events. The field 'timestamp' holds the UNIX timestamp of the event's creation time.
The query looks like this now:
$query = "SELECT * FROM events WHERE featured = 1 ORDER BY timestamp DESC LIMIT 2;";
Is there a way to syntax the query to return just one of those two events and display it right away, or should I go around it with php?
What is recomended here?
Use a ORDER BY RAND() LIMIT 1;, as per MySQL documentation for RAND() (near the bottom of the explanation). I'm not sure if you can do it without the nesting, but it shouldn't be all that expensive given that your nested table only has 2 rows.
SELECT * FROM
(SELECT * FROM events WHERE featured = 1 ORDER BY timestamp DESC LIMIT 2)
ORDER BY RAND() LIMIT 1;
Try:
SELECT * FROM
(SELECT * FROM EVENTS WHERE featured = 1 ORDER BY `timestamp` DESC LIMIT 2) AS temp
ORDER BY RAND() LIMIT 1
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