I've been trying to execute this line command:
START TRANSACTION; SELECT * FROM users where uid = 1 FOR UPDATE
in phpMyAdmin. It is throwing an error when I do so:
SQL query:
SELECT * FROM users where uid = 1 FOR UPDATE LIMIT 0, 25
MySQL said:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LIMIT 0, 25' at line 1
What I am trying to do is to implement exclusive record locking when an update query is executed.
Per https://dev.mysql.com/doc/refman/8.0/en/select.html, LIMIT needs to come before FOR UPDATE. So changing your query to the following should get you past this error:
START TRANSACTION; SELECT * FROM users WHERE uid = 1 LIMIT 0, 25 FOR UPDATE;
Related
I have a very simple query:
explain
select *
from mytable
limit 1
And I cannot see the execution plan, I receive this error:
Error Code: 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 'explain select * from mytable limit 1' at line 1
Why doesn't this work?
I am trying some sammple query in mysql client but the transaction syntax does not work]
MySQL database-2:3306 ssl TestDb SQL > START TRANSACTION
-> select * from mvcc where glyph = 'a'
-> COMMIT
-> ;
And it reports syntax error :
ERROR: 1064 (42000): 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 * from mvcc where glyph = 'a'
COMMIT' at line 2
This seems to be a very simple query and it's too easy to understand why it fails. I can run the select statement without any issue. Any idea?
You need a semicolon at the end of each of the queries.
START TRANSACTION;
SELECT * FROM mvcc WHERE glyph = 'a';
COMMIT;
Very similar to this question, just a clean cut.
Goal:
I need to use a DELETE statement with ALIAS and LIMIT. Optionally with USING, WHERE, or anything else, other workarounds as long as it works.
Cases 4, 6, 7, 8 would potentially be the solution for this question, but each of them returns an ERROR.
Cases 1, 2, 3, and 5 are shown just for the sake of this example (to fill up the matrix if you will).
Schema:
CREATE TABLE test (id int(10));
INSERT INTO test VALUES (1), (2), (3);
1) no alias, no limit - WORKS
DELETE FROM test;
2) no alias, limit - WORKS
DELETE FROM test LIMIT 1;
3) alias, no limit - ERROR
DELETE FROM test t;
Error : 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 't' at line 2
4) alias, limit (would be SOLUTION) - ERROR
DELETE FROM test t LIMIT 1;
Error : 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 't LIMIT 1' at line 2
5) alias+USING, no limit - WORKS
DELETE FROM t USING test t;
6) alias+USING, limit (would be SOLUTION) - ERROR
DELETE FROM t USING test t LIMIT 1;
Error : 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 'LIMIT 1' at line 2
7) alias, limit, where (would be SOLUTION) - ERROR
DELETE FROM test t WHERE 1=1 LIMIT 1;
Error : 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 't WHERE 1=1 LIMIT 1' at line 1
8) alias+USING, limit, where (would be SOLUTION) - ERROR
DELETE FROM t USING test t WHERE 1=1 LIMIT 1;
Error : 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 'LIMIT 1' at line 1
And what about this :
DELETE FROM t USING test t INNER JOIN (SELECT id FROM test LIMIT 1) t2 ON t.id = t2.id
I am trying to reset the auto increment value in one of my tables based on the number of rows currently in it. Here is the code I have so far.
SET #numrows = 0;
SELECT COUNT(*) total, #numrows := COUNT(*) + 1 numrows FROM maj_user ;
ALTER TABLE `maj_user` AUTO_INCREMENT = #numrows ;
This works great if I execute it in MySQL Workbench. However, I need to save this as an SQL file and execute it as part of a database import script. If I do this, I get this:
ERROR 1064 (42000) at line 39: 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 '#numrows' at line 1
Line 39 is the ALTER TABLE statement. Any ideas?
Can you change your syntax to skip actually setting #numrows? I'm not sure what the problem is but a workaround seems to be something like:
ALTER TABLE `maj_user` AUTO_INCREMENT = (SELECT COUNT(*) + 1 from maj_user);
This is my query -
$q = "CREATE TEMPORARY TABLE tmp SELECT * from category c WHERE category_id IN (SELECT category_id FROM category_to_store WHERE store_id = '".(int)$from_store_id."');
ALTER TABLE tmp drop category_id;
INSERT INTO category SELECT 0,tmp.* FROM tmp;
SET #last_id_in_category := LAST_INSERT_ID();
select #last_id_in_category;
DROP TABLE tmp;"
mysql_query($q);
I am getting this error on execution
Error: 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 'ALTER TABLE tmp drop category_id; INSERT INTO category SELECT 0'
at line 2 Error No: 1064
But when I run the query in database directly then I am not getting any error.
Please help me !
From the php docu on `mysql_query'
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database ...
So basically split your queries to multiple calls of `mysql_query' each with a single query and you should be fine.