DELETE statement with ALIAS and LIMIT - mysql

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

Related

MySQL 8.0 not being able to create any query plans

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?

Error Code: 1064 - You have an error in your SQL syntax

I have a very strange problem with this code:
Update creature_template SET health_min=(health_min * 0.03) where entry in (select entry from creature where rank ='1');
Update creature_template SET health_max=(health_max * 0.03) where entry in (select entry from creature where rank ='1');
Error occured at:2021-01-07 13:27:46
Line no.:1
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 '='1')' at line 1
I use mysql 8.x and I am a beginner. in mysql 5.x this problem does not exist.
Anyone if can help i would be grateful.
rank is a reserved word in MySQL since version 8.0.2. You need to escape it with backticks
... where `rank` = '1' ...

Error when trying to use SELECT...FOR UPDATE in MySql

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;

mysql update value if another one is not null

what's wrong with this query?
UPDATE `order` SET `total_no_vat` = IF(`total` IS NULL,NULL,(`total`/(1.10)));
I get an error that I cannot interpret:
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 ') )' at line 1
any clue?
You can simply do:
UPDATE `order` SET `total_no_vat` = `total`/(1.10);
If total is NULL then total/(1.10) evaluates to NULL.

Mysql syntax error : 'WHERE NOT LIKE'

I'm trying to track the page views by inserting only unique values every 24h.But when I run this query I get a syntax error.
insert ignore into profilepageviews values( '77.777.777.777' , CURRENT_TIMESTAMP, '5') where hitdate NOT LIKE '%2012-06-26%'
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 'where hitdate NOT LIKE '%2012-06-26%'' at line 1
You can not use WHERE clause with INSERT, You might want UPDATE