I have generated an sql to query a database table where posts from users are saved.But I am getting error executing it.I have tested it here , It also tells me that it is incorrect and the problem lies in users_user_id section.I can't figure out what might be wrong here.Can any one help me with this error.
My userpost table structure is following:
post_id,post,title,users_user_id
sql:
SELECT title FROM userpost LIMIT 5 OFFSET 10 WHERE users_user_id=9
LIMIT always come after all keywords.
Corrected SQL:
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
Generally, SELECT query has following sequence:
SELECT
Fields List
FROM
WHERE
GROUP BY (if needed)
ORDER BY (if needed)
LIMIT (This always come at end and if limit is not specified,
query will return all records from result set.)
You can set offset with limit as per below-
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 10, 5;
Where after limit first argumet is offset and 2nd is for no. of record means limit.
where should be use before limit
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
change:
SELECT title FROM userpost LIMIT 5 OFFSET 10 WHERE users_user_id=9
to:
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
Try this way :
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
where clause use after from.
LIMIT always come at last position
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5,10
I hope this will work
You need to change your Query. Always where condition place before LIMIT and OFFSET:
SELECT title FROM userpost WHERE users_user_id = 9 LIMIT 5 OFFSET 10;
limit should be after where condition
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
Try this way
And LIMIT should be after where condition
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
Write your where condition first then use limit. LIMIT always come after all keywords.
SELECT title FROM userpost WHERE users_user_id=9 LIMIT 5 OFFSET 10
Related
This question already has answers here:
Pagination using MySQL LIMIT, OFFSET
(4 answers)
Closed 4 years ago.
I am making an infinite scroll app in which records from database will be fetch. When the user clicks "Load More" button a function is called for retrieving data from database.
My problem is that when I am running this(below) query every Time I am getting the same records.
SELECT ArticleTitle, PreviewText, PreviewImage FROM Articles ORDER BY ID DESC LIMIT 4
regards....
You need to set a offset in hidden field.
On clicking load more, you need to get offset, calculate next batch and update offset in same hidden field
that new offset, should be passed to query.
your query looks like
SELECT ArticleTitle, PreviewText, PreviewImage
FROM Articles
ORDER BY ID DESC
LIMIT 0, 4
first time offset is 0
next time it will be 5 (if you are loading 5 records every time)
then next time it will be 10 and so on
You've to use the OFFSET and LIMIT together like this.
Explanation: The OFFSET value is also most often used together with the LIMIT keyword. The OFF SET value allows us to specify which row to start from retrieving data. By the way you can also omit the OFFSET keyword and use like this LIMIT 0,4 which tells to get 4 rows starting from 0. see more
SELECT ArticleTitle, PreviewText, PreviewImage
FROM Articles
ORDER BY ID DESC LIMIT 4 OFFSET 0
SELECT ArticleTitle, PreviewText, PreviewImage
FROM Articles
ORDER BY ID DESC LIMIT 4 OFFSET 5
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 have created a table that is similar to the one below, my goal is to LIMIT the result to 10 AND THEN return the id of the LAST result which is 10. I've tried doing, the query below but it keeps returning my the value of 15, instead of 10.
SELECT id FROM this_table WHERE value=value ORDER BY id DESC LIMIT 10.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
select max(id) from
(
SELECT id
FROM this_table
WHERE value = 'some_value'
ORDER BY id
LIMIT 10
) x
LIMIT can take two parameters.
Try
SELECT id FROM this_table WHERE value=value
ORDER BY id LIMIT 9,1
Read all about it.
EDIT: Oh, and loose the DESC part. It seems you don't really need it.
so suppose I have this query:
SELECT * FROM t WHERE j = k ORDER BY l
is there a way to have the query only return, for example the 20th to 40th records according to the ORDER BY statement rather than returning all the rows ordered by the ORDER BY statement?
SELECT * FROM t WHERE j = k ORDER BY l LIMIT 20, 20
Limit by 20 (the second one), starting from offset 20, the first.
Did you search in the documentation at all?
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
For compatibility with PostgreSQL, MySQL also supports the LIMIT row_count OFFSET offset syntax.
LIMIT + OFFSET
SELECT * FROM t WHERE j = k ORDER BY l LIMIT 19,21
Note:
the offset is zero based so 19 = start at row 20
and there are 21 rows between 20 and 40 inclusive
SELECT * FROM t WHERE j = k ORDER BY l LIMIT 20 OFFSET 20;
limit says you only need 20 rows, and offset says you don't need the first 20 rows.
Keep in mind that when the offset value is very large, your query will be very slow, since the sql server will need to scan the first <offset> rows to return <offset>+<limit> rows to you.
http://dev.mysql.com/doc/refman/5.0/en/select.html
sample: SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
so
SELECT * FROM t WHERE j = k ORDER BY l LIMIT 20,20; #retrieve rows 20-40
There's solution:
SELECT * FROM t WHERE j = k ORDER BY l limit 21, 20; (21 is number of rows and 20 is starting row, so you will retrieve rows from 20th to 40th if they exists)
I want to ascend MYSQL from a value in the database.
Hypothetically:
database with table tbl_numbers column numbers has 10 values in it 1-10.
I want to:
order by numbers desc {from 8} LIMIT 3
it would show
8
7
6
instead of:
order by numbers desc LIMIT 3
10
9
8
If this is possible what php and not mysql that's good too.
--update!--
The example I gave was way to easy, I really need to match the date() with the
dates of the entry's in the database and start the asend or decend from there any ideas? The date format is in 2010-06-18
ORDER BY numbers DESC
LIMIT 3,3
First argument for limit is (10 entries + 1) - 3;
second argument is the number of records to return
Alternatively:
WHERE numbers <= 8
ORDER BY numbers DESC
LIMIT 3
Something along the lines of
ORDER BY (numbers < 8) DESC, numbers DESC LIMIT 3
should do the job.