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 (...));
Related
Trying to delete a specific amount of rows in a MySQL query, I am able to SELECT whatever I want to delete with the following command, getting the results I need:
select * from ns_cos ns where ns.created_at <>
(select max(nsa.created_at) from ns_cos nsa
where nsa.month_year = ns.month_year)
However, when I try to delete the selected data with:
delete from ns_cos ns where ns.created_at not exists
(select max(nsa.created_at) from ns_cos nsa
where nsa.month_year = ns.month_year)
I get:
SQL Error [1064] [42000]: (conn=5159) 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 'ns where ns.created_at not exists (select max(nsa.created_at) from ns_cos nsa wh' at line 1
What am I doing wrong?
Your immediate issue is that not all MySQL versions support aliasing the table directly in delete from. Furthermore, though, you cannot re-open the table you delete from in the from clause.
Consider using the delete ... join syntax.
delete ns
from ns_cos ns
inner join (
select month_year, max(nsa.created_at) created_at
from ns_cos nsa
group by month_year
) ns1 on ns1.month_year = ns.month_year and ns1.created_at <> ns.created_at
EXISTS in there not posible use IN clause, but you need to enclose the table in a seprate select, so that mysql thinks it is another table
delete from ns_cos ns
where ns.created_at not IN (select max(nsa.created_at) from (SELECT * FROM ns_cos) nsa where nsa.month_year = ns.month_year)
This happens when you join tables from two schemas, no matter whether you take aliases for the table names or not.
Error
SQL 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 '...'
delete !ALIAS! from table ALIAS ...
Strangely, only with aliases, you can get around this error in a one-step SQL. Tested in MySQL 5.7.
Try this pattern, using the alias of the table you want to delete from between delete and from:
delete t1 from table t1
join table2 t2
on t1.id = t2.id
Your code would be:
delete ns from ns_cos ns where ns.created_at not exists
(select max(nsa.created_at) from ns_cos nsa
where nsa.month_year = ns.month_year)
Other steps I checked before finding out the alias trick (do not read)
I tried it with a view of the other schema's table in the same schema, that is not enough.
One way to get around this is to make a copy of the table in the same schema (and delete that copy afterwards).
You might also somehow make a full "linked server link" like in T-SQL [linkedservername].DB1.Schema.Table1 at How to join two tables if they are in different schemas Your code is on the same server, but this might still help jumping over to the other schema, untested.
I am trying to make a delete from joined same table like this:
DELETE FROM `sp10_seo_url` AS sp1 JOIN
(
SELECT seo_url_pk, COUNT(*) AS maxc
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING maxc > 1
) AS sp2
ON sp1.seo_url_pk = sp2.seo_url_pk
However I am getting a mysql 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 'AS sp1 JOIN ( SELECT seo_url_pk, COUNT(*) AS maxc FROM `sp10_s' at line 1
And I am not sure at all where the error is. The inner query runs just fine and returns the expected set of results. The "ON" keys are properly named (same since we are talking about the same table).
I guess the idea of the query is pretty clear (clean the table of different rows have the same set of values for the three "group by" columns. Is there another way to do this?
Thanks!
you can "cheat" mysql with a double indirection (as explained here Deleting a row based on the max value):
delete from `sp10_seo_url`
where seo_url_pk in (
select seo_url_pk from (
SELECT seo_url_pk
FROM `sp10_seo_url` sp1,
(
SELECT seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING count(*) > 1
) sp2
where sp1.seo_url_entity_type = sp2.seo_url_entity_type
and sp1.seo_url_entity_id = sp2.seo_url_entity_id
and sp1.seo_url_language_fk = sp2.seo_url_language_fk
) t
);
http://sqlfiddle.com/#!2/899ff5/1
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
I have two tables: tweets (a.o. containing column tweet_id) and references (containing columns tweet_id and class_id). Not all tweets might have been assigned a class_id yet. I was expecting this to work to extract all information of a tweet inclusing its class_id (if it has been set):
SELECT t.*, r.class_id FROM tweets t
LEFT JOIN references r ON (t.tweet_id = r.tweet_id)
ORDER BY t.tweet_id DESC LIMIT 50
However, I keep getting 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 'references r ON
(t.tweet_id = r.tweet_id) ORDER BY t.tweet_id DESC LIMIT 50' at line 1
I don't understand where the error is coming from. I have build the query according to the MySQL documentation (http://dev.mysql.com/doc/refman/5.0/en/left-join-optimization.html) but I am probably missing something?
Use this:
SELECT t.*, r.class_id FROM tweets t
LEFT JOIN `references` r ON (t.tweet_id = r.tweet_id)
ORDER BY t.tweet_id DESC
LIMIT 50
References is a reserved word, so it must be enclosed with backticks.
Take a look at this link to have a list of all reserved words.
REFERENCES is an SQL reserved word, so you need to use quotes :
SELECT t.*, r.class_id FROM tweets t
LEFT JOIN `references` r ON ...
By the way, SO colors your references word in blue, this should give you a clue.
references is a reserved word of MySQL
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.