SQL command not properly ended when using LIMIT - mysql

I am executing the below query in SQL Developer.
SELECT * FROM Person where person_name='rahul' order by created_time desc limit 10;
When I execute it, SQL Developer gives me below error.
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
I used the below link for reference.
https://www.techonthenet.com/sql/select_limit.php
I already tried
SELECT * FROM Person where person_name='rahul' order by created_time desc OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY;
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Error at Line: 1 Column: 75
Note that OFFSET is not treated as keyword.

Yes, that' cause Oracle don't have or support limit clause and thus you are getting ORA-00933 error. Rather, use FETCH .. OFFSET construct like
SELECT * FROM Person
where person_name='rahul'
order by created_time desc
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;
There are lot many similar question in StackOverflow. Should have tried searching the same. Example this one: How do I limit the number of rows returned by an Oracle query after ordering?

I have resolved the issue by using the below query.
SELECT * FROM Person where person_name='rahul' and rownum between 1 and 2 order by created_time desc;

If you get
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
It is probably because you aren't running Oracle 12. In that case, there are some workarounds, all involving subqueries and most sloppy. I used
select * from
( select column_name, ROWNUM rnum from
( select * from table_name)
where ROWNUM <= max_row )
where rnum >= min_row order by column_name;

Related

Warning "A non-numeric value encountered" when using parentheses with LIMIT statement in phpmyadmin

I am getting a PHP warning when running a query in phpMyAdmin that includes both parentheses and a LIMIT statement. For example:
(SELECT * FROM tableA LIMIT 1)
The warning is:
A non-numeric value encountered
I am using PHP version 7.2 and MySQL 5.6.42 and phpMyAdmin version 4.9.0.1
What might cause this warning?
EDIT:
My issue is not a duplicate of this, because no matter which column or table I use, I get the same warning
EDIT2:
Because people asked for an actual UNION query:
(SELECT name FROM tableA LIMIT 1)
UNION
(SELECT name FROM tableB LIMIT 1)
This was the scenario I encountered the warning.

Unexpected MYSQL UNION and extractvalue behaviour

I was attempting an error-based sql injection when I discovered a weird behavior that I can't explain. Here is a simple example:
SELECT * FROM users WHERE username = '' union SELECT extractvalue(rand(),concat(0x3a,(SELECT 1)));
Output: ERROR 1105 (HY000): XPATH syntax error: ':1'
But...
SELECT * FROM users WHERE username = '' union SELECT extractvalue(rand(),concat(0x3a,(SELECT 1 FROM users)));
Output: ERROR 1222 (21000): The used SELECT statements have a different number of columns
Now the second result is expected, because my users table have 3 columns. The first example is the one I don't understand.
Tested on 5.7.23-0ubuntu0.16.04.1
From what I could tell from messing around with it, because you are using a subquery, the expression is being evaluated in a different order. If you do this:
SELECT some_col FROM users WHERE username = '' union SELECT extractvalue(rand(),concat(0x3a,(SELECT 1 FROM users LIMIT 1)));
Essentially resolving the "different number of columns" exception, I think you'll get the same error as the first line again. At least in my testing, that's what happened. I guess it has to do with when the subquery is evaluated, cause that has to happen before the extractvalue() call can be completed.
I'm also pretty sure mysql doesn't read "SELECT 1" as a subquery, but rather probably just discards the "SELECT" entirely.

sql syntax error while using top query

I'm using the top query for my table but facing the error
You have an error in your sql syntax, chekck the manual that
corresponds to your mysql server version for the right sytntax to use
near '4 * from sitemain order by siteid desc limit 0,30' at line 1
here is the code which i used
SELECT top 4 *
FROM sitemain
ORDER BY siteid DESC
You are mixing MySQL and TSQL syntax together. The query obviously is MySQL (from the error message). What you want is
SELECT * FROM sitemain ORDER BY siteid DESC LIMIT 0,4
What you loking for is actually LIMIT Clause,
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):
Documentation:
https://dev.mysql.com/doc/refman/5.0/en/select.html
SELECT *
FROM sitemain
ORDER BY siteid DESC
LIMIT 4
SELECT *
FROM sitemain
ORDER BY siteid DESC
LIMIT 4
With MySQL you need to use the LIMIT command as explained here:
Limit is used to limit your MySQL query results to those that fall within a specified range. You can use it to show the first X number of results, or to show a range from X - Y results. It is phrased as Limit X, Y and included at the end of your query. X is the starting point (remember the first record is 0) and Y is the duration (how many records to display).
SELECT *
FROM sitemain
ORDER BY siteid DESC
LIMIT 4

MySQL error 1064 (v 5.0.96) GROUP BY clause

I'm used to running on an Oracle database, so I'm not really quite sure how to trouble shoot this problem. I've narrowed down a simple example of my query to the following:
SELECT 0 as gm_rowID,
'-ALL Grantmakers-' as grantmakerName
FROM dual
GROUP BY 2
phpMyAdmin runs the SQL with the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY 2 LIMIT 0, 30' at line 1
Oracle can run this query just fine. MySQL can run the query without the GROUP BY clause. Any ideas?
--Here is the entire query:
SELECT
p.grantmaker_rowid as gm_rowID,
gm.grantmaker_companyName as grantmakerName
FROM grantmaker_info gm, proposal_submission p
WHERE 0=0
AND p.grantmaker_rowid = gm.grantmaker_rowid
UNION
SELECT
0 as gm_rowID,
'-ALL Grantmakers-' as grantmakerName
FROM dual
ORDER BY 2
GROUP BY 2
LIMIT 0 , 30
Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column
names, column aliases, or column positions. Column positions are
integers and begin with 1
From: http://dev.mysql.com/doc/refman/5.0/en/select.html
Unless you only have 1 column in that table, it should run fine. My suggestion however would be to reference the column name (or alias) of whatever you're trying to GROUP BY.
edit: My only other suggestion is to include the SHOW CREATE TABLE output for that table.
edit2: Ok I see you've updated your question. Why not instead of ORDER BY 2, you ORDER BY grantmakerName (if that's the column you want to order by?)

MySQL Is doing subquery after LIMIT syntax possible? If not, why?

I have MySQL Server version 5.1.53. I was looking for an hour to answer this question by myself. Including read the documentation itself at http://dev.mysql.com/doc/refman/5.1/en/select.html
Currently, I run this query.
SELECT dv2.timestamp
FROM data_val AS dv2
WHERE dv2.timestamp > '2011-06-10 22:26:25' ORDER BY dv3.timestamp DESC
LIMIT 1
Then I was trying to eliminate the ORDER BY syntax by determining the calculation of MAX_QUERIES minus 1. By doing that I could write,
SELECT (COUNT(*)-1) total
FROM data_val AS dv2a
WHERE dv2a.timestamp > '2011-06-10 22:26:13'
Finally the query becomes,
SELECT dv2.timestamp
FROM data_val AS dv2
WHERE dv2.timestamp > '2011-06-10 22:26:13'
LIMIT (
SELECT (COUNT(*)-1) total
FROM data_val AS dv2a
WHERE dv2a.timestamp > '2011-06-10 22:26:13'
), 1
And the error is:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( SELECT (COUNT(*)-1) total FROM data_val AS dv2a ' at line 4
I also tried to put the subquery after OFFSET syntax. but still error.
Do you have any idea why my sub-query doesn't work?
I need technical details with short,
simple, and clean explanation.
From the MySQL manual: http://dev.mysql.com/doc/refman/5.5/en/select.html
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, with these exceptions:
Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.
Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.
The MySQL query optimizer needs to resolve the limit parameters to a constant before running the query, or it will not know how many rows to return.
You can't imbed a query result for a limit parameter