I am being stumped over this problem, and I can't seem to figure it out, hoping someone here can do make heads or tails over this.
When I use the following to query my database:
$q = "SELECT * FROM items WHERE (searchable='1' and title LIKE'%".$_srch."%')";
the items show up they way they are supposed to. But when I add limit 10, 5 to the end:
$q = "SELECT * FROM items WHERE (searchable='1' and title LIKE'%".$_srch."%') limit 10, 5";
The query shows up nothing. I have tried everything I can think of, but I must have missed something. Someone help?
Thanks
By using LIMIT 10, 5 you're stating that you want the database to start displaying from the 11th row, and display 5 rows. (11th, 12th, 13th, 14th, 15th) - It might be possible that you actually, don't have enough rows.
Try something like, LIMIT 0, 5 - this will display the first 5 rows from the beginning, but 10, 5 will only work if you have more than 10 rows for the search query.
Read more here: http://www.mysqltutorial.org/mysql-limit.aspx
The Limit clause arguments are offset and number of items respectively.
So the query you write would show 5 rows starting from the 10th row.
How many rows does your original query show. If it is less then 10 rows then the limit query you provide wont work as the number of rows are less.
Related
I'm trying to figure out the fastest way to get x rows from a table that are offset by x rows and ordered by a date column.
The problem I have is I'm paginating the rows from the query into pages of 10 rows per page, but I only need the nth page.
For example if I only need page 4 from the table, I need to select all the rows:
SELECT * FROM posts ORDER BY date
Then I need to paginate the array using PHP and get the 4th page (if it exists). This is less than ideal as it seems a waste to have to get the whole table.
Is there a better way to query the table in this situation?. For example if I have 10 posts per page and I want the the 4th page, is there a way to offset the query so it starts from the 30th row? (and ordered by date).
You're looking for LIMIT
SELECT * FROM posts ORDER BY date LIMIT 10, 20
Where 10 is offset and 20 is the number of rows
LIMIT is the answer. According to MySQL documentation,
The LIMIT clause can be used to constrain the number of rows returned
by the SELECT statement. LIMIT takes one or two numeric arguments,
which must both be nonnegative integer constants (except when using
prepared statements).
With two arguments, the first argument specifies the offset of the
first row to return, and the second specifies the maximum number of
rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
I apologize if this question has been asked already, I'm sure it has I just was not able to find a similar question/answer that solved my problem.
I am pulling data from PostgreSQL and I have an SQL statement similar to:
$SQL = "SELECT * FROM my_view_table WHERE id='".$my_id."'";
From this I run my query. The request pulls from a view in PostgreSQL which looks through more tables. That is correct. I'm not sure how to limit the number of rows to display. I receive about 1,000 rows of information with about 20 columns of data so it takes an incredible amount of time to query.
Is there a way to ask for rows 0 - 100, then 101 - 200, etc, so I can pull 100 at a time to display? I know I'll have to use a little code to keep track of the count, but I just need some SQL help with querying "x to y" rows.
Thank you for the help with this issue. (If there is another very similar question that has already been answered a link to that would be a sufficient answer!)
I've posted the answer to my question down below.
I found the answer to this question from gnarly's suggestion of LIMIT on SQL
$sql = "SELECT * FROM my_table LIMIT X OFFSET Y";
Where LIMIT only gives the X number of rows you want, and OFFSET gives the Y starting point. So showing rows 0 through 30:
$sql = "SELECT * FROM my_table LIMIT 30 OFFSET 0";
And showing rows 31 through 60:
$sql = "SELECT * FROM my_table LIMIT 30 OFFSET 30";
I'm building an infinite scroll for a photo gallery of a website.
It starts with 6 photos loaded and should load 3 more photos every-time the user reach the end of the page.
So, me first mysql is:
SELECT * FROM tb_galeriaarte ORDER BY datafoto DESC LIMIT 0,6
When the user reach the end of the page, I'm using the following mysql command to add 3 more photos:
SELECT * FROM tb_galeriaarte ORDER BY datafoto DESC LIMIT 6,9
The problem is that it returns 4 records instead of 3 and I have no idea why it happens!
Somebody can help me with that? what am I doing wrong?!
To add three more photos, the second limit would be:
limit 6, 3
The arguments are offset and number of records. You are asking for 9 records starting on the 7th (the offset starts counting from zero).
You should learn more about MySQL.
LIMIT [start, ] count
So, in your second case it will be
LIMIT 6, 3
Here you can learn more about LIMIT: doc
Just write
SELECT * FROM tb_galeriaarte ORDER BY datafoto DESC LIMIT 6,3
2nd digit of LIMIT points quantity but not top limit
I'm going to refrain from posting my massive code block and just start exactly what I nailed the issued down to.
I'm running this query:
...
SELECT id, Title, images, recdate, 'item' AS type
FROM ads_list
WHERE to_days(now())<= (to_days(recdate)+14)
ORDER BY recdate DESC
LIMIT $offset, $listsperpage
...
This is for a pagination script. My $offset is just a count of the current records position and $listsperpage is set to an arbitrary number per pages (and this is set to 24). So there are supposed to be 24 results per page...
Everything is working perfectly fine EXCEPT, when I run my query in DESC by recdate sort, there are only 23 records outputted in my array on the first page??
And if I run with ASC sort, there are 24 records that display as expected on each page.
My recdate field is formatted like so:
2012-01-14 07:10:33
2012-01-14 07:10:35
2012-01-14 07:10:38
2012-01-14 07:10:30 ...
I also tried to do a DESC sort by id (auto) and still the first result array only contains 23 records, but the total array are all expected records.
I found the "missing" record from the first result is actually pushed forward, so its as if there is just a gap in the results with DESC. And that array is pushed completely forward to the last results page.. So all items are shifted one slot it appears.
I did not want to post up my entire code block because it's a lot, and I've spent a few hours testing and trying variations of my code, so I really think it's something to do with the SQL sort.
I just can't see my DESC would cause this oddity and ASC sort shows as normal.
Finally figured this nightmare out!
I still can't explain why it happens, but at least I am getting full expected results now.
In my while loop, I check the $list["images"] != "". I turns out when using DESC sort on recdate, there is a blank array value, so it evaluates to true and "drops" that listing from the page. So I added $list["images"] == "" and now the image and data displays for that array item.
I am trying to implement the pagination in php. I am using the Mysql as back end database. I am trying to implement the pagination logic.
I would be having lots of record. But the user will see only 10 at a time.
Now to show the first page, i do a
SELECT * from USERS LIMIT 10.
Now to get the next 10 and the subsequent 10 records i am not able to write a query. Please help me fetch the in between records to support pagination logic. Also provide if any other suggestions for pagination.
You should use the OFFSET option.
SELECT * FROM Users LIMIT 10 OFFSET 10 (or 20, or 30);
That way you just pass the start position in the request when you hit next (or the page number) and you'll retrieve the records you want.
MySQL's limit feature can take two arguments:
select * from USERS limit 10,10
The above would retrieve 10 rows starting at row 10. Bear in mind that the MySQL row offset is 0 based, not 1. The first argument is the starting row, the second is the page size.
Also, if your page size is consistent, all you need to do is pass in the current page (default to zero). That would then allow you to specify the start row as a page * size.