Say I have 50 rows in my database table and I wanted to start retrieving database entries starting with row 9.
How can I retrieve data from a database table starting at row 9?
My code for pulling data from my db table:
$sql = mysql_query("SELECT * FROM listheadlines ORDER BY id DESC LIMIT 20");
You can use OFFSET keyword like this:
$sql = mysql_query("SELECT * FROM listheadlines ORDER BY id DESC LIMIT 20 OFFSET 9");
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter ( mysql.com ).
"SELECT * FROM listheadlines ORDER BY id DESC LIMIT 9, 18446744073709551615"
Or to get only 20 rows starting at offset 9:
"SELECT * FROM listheadlines ORDER BY id DESC LIMIT 9, 20"
Try this
mysql_query("SELECT * FROM listheadlines ORDER BY id Desc").setFirstResult(9);
Please check this once
How to fetch rows from middle of the table in mysql using hibernate?
Related
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
Im making a query to the database where on the first query I limit the results to 4 by doing :
$result = mysql_query("SELECT * FROM members limit 4");
This gets me my first 4 entries in the DB and On my next query I want to get the next four what kind of approach should I take?
$result = mysql_query("SELECT * FROM members limit 4 count= 5");
LIMIT can take two arguments:
LIMIT {[offset,] row_count | row_count OFFSET offset}]
In your case you would do:
SELECT * FROM members limit 4,4;
But to do this consistently, you really should use some sort of ORDER BY, otherwise you are not assured to get consistent results.
sqlfiddle demo
You're close:
$result = mysql_query("SELECT * FROM members limit 3,4");
This tells MySQL to start returning rows at the fourth row (remember, mysql starts counting at zero so 0 is the first row and 3 is the fourth row) and return four rows total.
SELECT * FROM your_table LIMIT 3, 4;
I have a table containing rows with timestamped.
Normally if I want to get the latest 20 rows out according to the time. I use:
$sql = "SELECT *
FROM comment
ORDER BY time DESC
LIMIT 20";
But now, I want to get the latest comments AFTER the latest 20 rows and LIMIT to 10. That means rows 21-30.(of course , everything is according to timestamp)
How can I do that using MySQL?
MySQL has a built-in offset that you can use with LIMIT:
$sql = "SELECT * FROM comment ORDER BY time DESC LIMIT 10, 20";
Also, refer to this SO post: MySQL LIMIT/OFFSET: get all records except the first X
$sql = "SELECT * FROM comment ORDER BY time DESC LIMIT 20, 10";
Hope it will select from 21 to 30 records
sql = "SELECT * FROM comment ORDER BY ID DESC LIMIT 20, 10";
Try a mixture of limits
$sql = "select * from (SELECT * FROM comment ORDER BY time DESC LIMIT 30) as A order by time ASC limit 10";
The mysql built in offset method others have posted looks better though.
There are two options:
Get 30 rows and use PHP to split the result set into a group of 20
and a group of 10.
Send two queries, one for 20 and one for 10 rows.
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.
I want to return only the last 15 rows in my table, then the 15 before that.
Unfortunately while($rows = mysql_fetch_assoc($result)) where the query is SELECT * FROM table returns the data in all rows.
I thought about doing something like:
In my insert script
SELECT * FROM table then $selection_id = mysql_num_rows($result)-14 before inserting any data, then adding column named selection_id which would contain $selection_id, thus each set of 15 rows would have the same selection_id.
In my select script
SELECT * FROM table then $num_rows = mysql_num_rows($result)/15 then SELECT * FROM table WHERE selection_id='$num_rows' and SELECT * FROM table WHERE selection_id='$num_rows-1'.
I could then perform while(..) on both results as usual.
However, I'm not sure this is the most efficient way (chances are it's not), so if not, I'd really appreciate some suggestions to cut down the amount of code I'll have to use :)!!
Use a LIMIT clause in your query, order by your auto-incrementing primary key in descending order. E.g.
SELECT * FROM `table` ORDER BY `selection_id` DESC LIMIT 0,15
...will get the last 15 rows, and:
SELECT * FROM `table` ORDER BY `selection_id` DESC LIMIT 15,15
...will get the 15 rows before that.
Selecting the last 15 rows:
SELECT *
FROM `table`
ORDER BY `id` DESC
LIMIT 0,15
Selecting the 15 rows before the previous ones:
SELECT *
FROM `table`
ORDER BY `id` DESC
LIMIT 15,15
And you can continue in a while cycle.
You need to check out mysql LIMIT. To get the last 15, you'd need to know the number of total rows.
$offset=$rowcount-15;
$sql="SELECT * FROM mytable LIMIT $offset,15";
This is just for example, you'd want to make sure there are at least 15 rows, I'm not sure how mysql would deal with a negative offset. I'll let you figure out how to count the rows.
Edit:
Oh, haha, you could also just sort it descending, that will save you having to query twice.
SELECT * FROM mytable ORDER BY id DESC LIMIT 15;