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

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

Related

LIMIT in MySQL Update Row

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.

SQL execution error

I try this query
INSERT INTO shop.product(prod_id,model,desc) SELECT product.id_prod,prod_lang.name,product.ref from product left join product_lang on product_lang.id_prod = product.id_prod
However I got this error
SQL execution error # 1065.Response from the database:
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 product.id_prod,product_lang.name,product.ref from' at line 1
Two problems:
DESC is a reserved keyword. Use backquote (``) for desc.
Change prod_lang to product_lang in the query.
Solution:
INSERT INTO shop.product (prod_id,model,`desc`)
SELECT product.id_prod,product_lang.name,product.ref
from product left join
product_lang on product_lang.id_prod = product.id_prod
Note:
It is a good practice to use backquotes for all columns eventhough it is not a reserved keyword.

Delete mysql with join and limit

I have such sql-query:
DELETE `LINK_LA_TYP`
FROM
`LINK_LA_TYP`
JOIN `LINK_ART` ON `LINK_LA_TYP`.LAT_LA_ID = `LINK_ART`.LA_ID
JOIN `ARTICLES` ON `LINK_ART`.LA_ART_ID = `ARTICLES`.ART_ID
WHERE
(
`ARTICLES`.ART_SUP_ID IN(
10008,
10439,
11005,
...
...
441
)
)
LIMIT 50000;
But i get error.... From mysql-doc i get that with delete+join+limit i will get errors....
But how can i change my code? (new to mysql and sql in all). How to change my code? To limit rows to delete....
Also in phpmyadmin i get
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 50' at line 1
From the docs 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. dev.mysql.com/doc/refman/5.0/en/delete.html – Michael Berkowski 2 mins ago
I concur with this. Additionally,
The DELETE command cannot have a LIMIT clause. Also, generally speaking you cannot DELETE from multiple JOINed tables.
It MIGHT be possible to rewrite the statement using a subselect something like:
delete from LINK_LA_TYP
where LAT_LA_ID in
(select LA_ID
from LINK_ART
join ARTICLES on ARTICLES.ART_ID = LINK_ART.LA_ART_ID
where ARTICLES.ART_SUP_ID in (...));

#1064 When Deleting item from Table

I recently upgraded to MYSQL 5. That is latest version that webhost allows. When trying to update a table or delete an entry this is the error I receive while in phpmyadmin.
Error
There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem
ERROR: Unclosed quote # 101
STR: '
SQL: DELETE FROM inmates WHERE inmates.counties_id = 33 AND CONVERT(inmates.link USING utf8) = \'http://inmate1.riversidesheriff.org/iis/\' LIMIT 1
SQL query:
DELETE FROM inmates
WHERE `inmates`.`counties_id` = 33
AND CONVERT(`inmates`.`link` USING utf8) = \'http://inmate1.riversidesheriff.org/iis/\'
LIMIT 1
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 '\'http://inmate1.riversidesheriff.org/iis/\' LIMIT
1' at line 1
and here is the query
SELECT * FROM `inmates` WHERE 1
any help is greatly appreciated
thank you.
Lose the backslash before the single-quotes: they are not required.
By that I mean, change this:
\'http://inmate1.riversidesheriff.org/iis/\'
To this:
'http://inmate1.riversidesheriff.org/iis/'

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.