LIMIT in MySQL Update Row - mysql

I am trying to Update Some row in my database. If I run without limit its working fine but if I run it with limit its giving me error like below
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 '35' at line 1
My Query is like below
UPDATE number_list SET sync = 0 WHERE server = 1 ORDER by id ASC LIMIT 0,35
Let me know if someone can correct me.

You can use limit in an update (in MySQL) but not an offset. So just do:
UPDATE number_list
SET sync = 0
WHERE server = 1
ORDER by id ASC
LIMIT 35;
This is a bit subtle, because SELECT supports offsets. However, it is clear in the syntax diagram for UPDATE.

Related

Syntax Error with ASC, DESC with MySQL 8.0

I recently migrated my clients legacy infrastructure which entailed moving there old MySQL 5.0 database into a MySQL 8.0 database (We had some other issues while migrating to Server 2016 that meant we had to do this)
Anyway the client has contacted me today as one of there queries is not working and I can't get to the bottom of it.
It appears as if the following line is used on most / nearly all queries are not working.
GROUP BY L.GrowerID, L.DCropID, C.Variety, C.Crop, L.SizeID DESC, S.Size;
Full Query:
SELECT Cu.Customer, L.DCropID, C.Crop, C.Variety, D.Clone, G.Grower,
S.Size, SUM(L.EndTubers) AS "Tubers", FORMAT(SUM(L.EndWeight),2) AS "Weight"
FROM PotatoLabels.Crop C, PotatoLabels.PLabel2012 L, PotatoLabels.Sizes S,
PotatoLabels.DCrop D, PotatoLabels.Customers Cu, PotatoLabels.Growers G,
PotatoLabels.mmGrades M
WHERE (D.DCropID > 96534 AND Cu.CustomerID = 9 AND G.GrowerID = 1 )
AND L.CustomerID = Cu.CustomerID
AND D.DCropID = L.DCropID
AND C.CropID = D.CropId
AND L.SizeID = S.SizeID
AND L.GrowerID = G.GrowerID
GROUP BY L.GrowerID, L.DCropID, C.Variety, C.Crop, L.SizeID DESC, S.Size;
If I remove
GROUP BY L.GrowerID, L.DCropID, C.Variety, C.Crop, L.SizeID DESC, S.Size;
It comes back with completely blank results but it does run with no syntax errors.
Also worth mentioning, if I remove the ASC and (or) DESC the query runs without ascending & descending filtering applied - Which is slightly better not not sufficient for my client.
Please see below syntax errors I receive when using some of the faulting queries.
[Err] 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 'DESC, S.Size' at line 12
[Err] 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 'ASC, L.SizeID DESC WITH ROLLUP' at line 22
I am no MySQL expert this has merely landed on my desk... I'm sure you understand.
Any help would be great.

MySQL phpmyadmin - SELECT FOR UPDATE not recognised

I'm writing a PHP program and wanna to implement row-level locking to avoid concurrent user update/delete for the same record.
But I hit error "Unrecognised keyword" when using SELECT FOR UPDATE. Table type is innoDB.
Am i missing any setup for my database?
SELECT * FROM companyTable
WHERE companyId = "0000001"
FOR UPDATE;
Error
Static analysis:
1 errors were found during analysis.
Unrecognized keyword. (near "FOR" at position 57)
SQL query: Documentation
SELECT * FROM companyTable WHERE companyId = "0000001" FOR LIMIT 0, 30
MySQL said: Documentation
#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 'LIMIT 0, 30' at line 3
There Is an Syntax Error Try this:
Its Select All Records From 1 - 30
SELECT * FROM companyTable WHERE companyId = "0000001" ORDER BY id LIMIT 30;
First problem seems to be, that in your SQL query is missing keyword UPDATE
SELECT * FROM companyTable WHERE companyId = "0000001" FOR LIMIT 0, 30
And the second issue can be, that syntax SELECT ... FOR UPDATE in MySQL doesn't support LIMIT.
So your SQL query should be:
SELECT * FROM companyTable WHERE companyId = "0000001" FOR UPDATE
In phpmyadmin, it is difficult to remove auto added LIMIT clause - try another MySQL client.
PhpMyAdmin is not build to handle(Test) transaction control, use Mysql Console or php session instead
and use "begin" to start the Transaction before the code
begin;
select * from `tblx` where `idx`=1 for update;
update `tblx` set `field`='xx' where `idx`=1;
commit;

in mysql where command not work

Hi when i call this query for select this work good and make output
select SUBSTR(img_address1,LOCATE('/',img_address2)+1,36)fROM content
but when i make this query this make a error
SELECT id from content WHERE ((SUBSTR(img_address2,LOCATE('/',img_address2)+1,5) LIKE'salam')
and this make error too
SELECT id from content WHERE ((SUBSTR(img_address2,LOCATE('/',img_address2)+1,5) = 'salam')
and my error is :
SELECT id from content WHERE ((SUBSTR(img_address2,LOCATE('/',img_address2)+1,5) = 'salam') LIMIT 0, 25
MySQL said: Documentation
#1064 - 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
There is an extra open bracket ( before SUBSTR. Removing it will fix the error.
SELECT id from content WHERE (SUBSTR(img_address2,LOCATE('/',img_address2)+1,5) = 'salam') LIMIT 0, 25

DELETE FROM `table` AS `alias` ... WHERE `alias`.`column` ... why syntax error?

I tried this with MySQL:
DELETE FROM `contact_hostcommands_relation` AS `ContactHostCommand` WHERE (`ContactHostCommand`.`chr_id` = 999999) LIMIT 1
And I get this:
#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 (`ContactHostCommand`.`chr_id` = 999999) LIMIT 1' at line 1
Note: This query is automatically generated and conditions are based on table aliases.
Why I get this error?
Is there any way to use table aliases in where clause?
Is this MySQL specific?
What #Matus and #CeesTimmerman said about MSSQL, works in MySQL 5.1.73 too:
delete <alias> from <table> <alias> where <alias>.<field>...
You can use SQL like this:
DELETE FROM ContactHostCommand
USING `contact_hostcommands_relation` AS ContactHostCommand
WHERE (ContactHostCommand.`chr_id` = 999999)
LIMIT 1
You cannot use AS in a DELETE clause with MySQL :
DELETE FROM `contact_hostcommands_relation` WHERE (`chr_id` = 999999) LIMIT 1

What is wrong with this MySQL query?

DELETE
LIB, REL
FROM
test_library_song LIB
INNER JOIN
test_relation REL
ON
LIB.book_id = REL.book_id
WHERE
REL.user_id = '1'
AND
REL.book_id = '400'
LIMIT 1
It throws an 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 'LIMIT 1' at line 13
If I remove the LIMIT 1, it works, but I want it on for security measures.
As I see it, the LIMIT is only valid for single table deletions
http://dev.mysql.com/doc/refman/5.0/en/delete.html
For the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.
LIMIT cannot be used on a multi-table delete. See the DELETE syntax documentation.