PHP - Limit SQL results and show them by id - php

How can you LIMIT SQL results and show them by order id once you refresh the page? For en example:
Refresh #1;
Show Query with id 20
Rrefresh #2
Show query with id 21
Rrefresh #3
Show query with id 23
etc etc.

You can store the last showed id in a Session and than increment it on the next show.
to limit result you can use LIMIT:
SELECT * FROM `table` LIMIT 5
to order the result use ORDER BY:
SELECT * FROM `table` ORDER BY `id` ASC

Did you mean - how to show latest N items ordered by id in descendant order?
select *
from table
order by id desc
limit 0, 10
(N = 10 in this case)

Related

Pagination query using SQL

I have a table called 'items'
It has the following columns: id, name, stock, price, and category_id.
My query is
SELECT * FROM items WHERE category_id = {$category_id} LIMIT 10;
my logic for next page is store all ids in a variable called $oldIds and the next query is:
SELECT * FROM items WHERE category_id = {$category_id} AND id NOT IN ($oldIds) LIMIT 10
for another page is i store again the ids from 1st page and 2nd page do again the query
SELECT * FROM items WHERE category_id = {$category_id} AND id NOT IN ($oldIds) LIMIT 10
Do i continue using this style of query or is there a better way of querying?
for mysql
First statement should be:
mysql_query("SELECT * FROM {$statement} ORDER BY datetime ASC LIMIT {$limit} OFFSET {offset}
if 10 records per page is what you are looking for and you want to show records on page 2 i.e. records 11-20 your query will look like:
mysql_query("SELECT * FROM {$statement} ORDER BY datetime ASC LIMIT 10 OFFSET 10

Pulling specific data on top and restricting its limit in MySQL PHP

I have following sql query which pulls data with the id 627 to top and then it displays data with other ids
$sql_query = "select * from listing ORDER BY case when listing.makaan_id='627' then 1 else 2 end, listing_id DESC LIMIT {$start}, {$limit}";
im using php pagination system to paginate the results where i set the pagination limit to 10. Now i want to show only 4 results on top with the id 627 and rest with other ids, so in a set of 10 results 4 would be from the id 627 and other 6 will be from other ids, do i have any solution for this without writing another separate sql statement?
You can try something like this
SELECT *
FROM listing
WHERE makaan_id = 627
ORDER BY listing_id DESC
LIMIT 4
UNION ALL
SELECT *
FROM listing
WHERE makaan_id <> 627
OR makaan_id IS NULL
ORDER BY listing_id DESC
LIMIT {$start} - 4, {$limit} - 4

Negative limiter MySQL

My code get the last 10 values from a table. This table has te structure id,text and by the moment it has 20 rows. I use this piece of code
<?php
$query = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT 10");
// Fetching data stuff here
?>
and it returns the data for IDs 11-20. (The last 10 of the 20)
I want to get the previous 10 values from this 1-10 via AJAX. I thought maybe this will work
$previous_id= $_GET["last"]; // This time it will be 11
mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT $previous_id, -10");
But I'm doesn't, any suggestion?
Thanks
PS: This is not a gimme teh codez question, I just want to know how to make that query work.
If I understand you correctly you want to get the 10 rows before the row with id = $previous_id, ordered by descending ID.
If that's the case your best bet would be to use a WHERE condition. As you are ordering by id DESC you want the first 10 rows with id > $previous_id:
SELECT * FROM `table` WHERE `id` > $previous_id ORDER BY `id` DESC LIMIT 10
These will be the 10 rows before $previous_id in your original query.
Simply use positive number instead of -10:
"SELECT * FROM `table` ORDER BY `id` DESC LIMIT $previous_id, 10"
Let's suppose $previous_id is 5, the query would be:
"SELECT * FROM `table` ORDER BY `id` DESC LIMIT 5, 10"
The returned rows will be starting from 5 and 10 records.

MYSQL select newest posts from tables

I want to select the newest posts from users, I am looking for the most efficient way to do this.
Currently this selects the first post, not the last:
$query = mysql_query("
SELECT *
FROM posts
WHERE toID=fromID
GROUP BY fromID
ORDER BY date DESC LIMIT 3");
Table Structure:
Table: posts
id ToID FromID Post State Date
1 1 1 Hey 0 1325993600
2 1 6 okay yeah 0 1325993615
3 1 2 again 0 1325994600
4 6 6 yeah2 0 1325995615
so from this above example it would return id: 1 and 4.
toID=fromID is just to get the post that is a status message, meaning the user posted something on their own page, not someone elses.
I want to get the most recent status from the last 3 users that have updated their status.
The ID thing would still work theoretically, provided that the ID's never change...
I would recommend using a timestamp field in the table structure called "date" and use the "CURRENT_TIMESTAMP" as default value, this will auto-populate the date/time on the record upon insert...
Order by this field DESC, limit x
Also, I have experienced many cases of the wrong data appearing thanks to grouping... Make sure your data is correct before ORDER BY and LIMIT is applied
For getting posts from user1 to user1 there's no need to group by:
SELECT * FROM posts
WHERE toID=fromID
ORDER BY date DESC LIMIT 3
For getting posts from * to user1:
SELECT * FROM posts
WHERE toID="USER1_ID"
ORDER BY date DESC LIMIT 3
For getting posts from * to user1, only unique users:
SELECT * FROM posts
WHERE toID="USER1_ID"
GROUP BY FromID
ORDER BY date DESC LIMIT 3
Somtimes you will run into the problem where GROUPED records are not ordered by ORDER BY, because the ORDER BY is applied to the result AFTER the grouping is applied... To achieve a workaround:
SELECT * FROM (
SELECT * FROM posts
WHERE toID="USER1_ID"
ORDER BY date DESC
) as `derived` GROUP BY FromID LIMIT 3
To Get the last 3 users who have most recently sent themselves a post:
SELECT * FROM (
SELECT * FROM posts
WHERE toID=fromID
ORDER BY date DESC
) as `derived` GROUP BY FromID LIMIT 3
try this query.
$query = mysql_query("SELECT * FROM posts WHERE toID=fromID GROUP BY id ORDER BY date DESC LIMIT 3");

MySql randomize the last 10 rows

I need help on how to randomize the last 10 rows of MySql records.
$mysqld = mysql_query(select * from table where amount > amount2 and code = '$code' order by time DESC limit 1);
From the statement above I need to randomize the last 10 rows ordered by time but limited only 1 to display.
EDIT: In other words, I need to have the table ordered by time and then I need to focus on the last 10 rows. From these last 10 rows, I need to pick one and it must be random, which one I get.
Is this possible?
Thanks
Assuming that time is the time when record was inserted, this will get you the latest 10 rows from the table:
SELECT * FROM `table` WHERE `amount` > `amount2` AND `code` = '$code'
ORDER BY `time` DESC LIMIT 10
Now, you can use the result as a temporary table, sort it randomly (as it's only 10 rows) and return one row:
SELECT * FROM (
SELECT * FROM `table` WHERE `amount` > `amount2` AND `code` = '$code'
ORDER BY `time` DESC LIMIT 10
) AS temptable
ORDER BY RAND()
LIMIT 1
Try....
SELECT * FROM (SELECT * FROM yerTable ORDER BY id DESC LIMIT 10) AS tmp ORDER BY RAND() LIMIT 1
Obviously replace id with any other distinct column if preferred.

Categories