Mysql delete with join and limit - mysql

I have this 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,10097,10669,11100,80,10912,10683,10675,10194,11196,1166,10730,10248,10870,11200,11059,247,10121,10911,489,10724,496,10093,10205,1318,10953,11199,11047,128,114,194,10865,11058,10345,1286,10667,10064,11077,10622,11205,10917,10344,495,10709,10954,10744,304,10957,10447,10764,10129,10862,10918,10731,11115,10095,10859,10580,1345,10177,10323,144,11182,10132,256,10941,58,10006,10017,10780,10765,10665,11110,10714,10224,750,10267,10179,10725,10774,11063,10868,10103,10676,10057,10649,255,10322,11022,309,10754,11121,10801,10018,11004,10245,146,11056,381,10781,10699,11120,11126,830,10240,11162,10436,10584,10342,10861,11190,10721,11171,10564,10545,94,10087,73,10755,10869,10547,10706,10346,444,426,10059,153,122,10674,64,113,11101,10231,10337,806,11117,10385,251,11188,491,11192,100,10792,10069,10864,11099,10246,10178,10758,10568,10230,10124,10384,10782,10726,384,10670,305,10763,10768,10585,10394,10552,498,10677,1348,168,10814,10582,10382,11093,11173,10381,427,441)) limit 50000;
but why do I get 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 50' at line 1
What's wrong? And how to limit delete query entries to delete?

From the manual:
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.
You are better off SELECTing the ids you want to delete with said limit and then deleting those.

You need to check the syntaxis of DELETE if you try to delete with joins, you better create the tables with CASCADE on update.

Related

Query to update and delete records in two different tables

I want to write a query that deletes a record from a table and updates a record in anther one. This is my query:
DELETE FROM borrowed_books a WHERE a.id = '$id'
AND
UPDATE books b SET b.nr_copies=b.nr_copies+1 where
b.id_book=a.id_book
The error in console says:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version
I am using mySQL and XAMPP.
You can't do two action in a query
you must use two query
DELETE FROM borrowed_books a WHERE a.id = '$id'
;
UPDATE books b
INNER JOIN borrowed_books a
SET b.nr_copies=b.nr_copies+1
where b.id_book=a.id_book
and a.id = '$id'
;
using update with join
eventually you can check for your mysql driver for multiple query in a command

Difference between single-table and multiple-table syntax?

I'm trying to understand how MYSQL determines single-table and multiple table syntax but I can't find this information. The documentation explains how MYSQL handles the two, but it doesn't explain what determines them.
Documentation says:
You can specify multiple tables in a DELETE statement to delete rows
from one or more tables depending on the particular condition in the
WHERE clause. However, you cannot use ORDER BY or LIMIT in a
multiple-table DELETE.
Take this query for example:
DELETE table FROM table
INNER JOIN other ON other.column = table.column
WHERE other.column2 = ?
LIMIT 1
Is this single or multiple table syntax? There's a JOIN, so you could lean toward multiple, but it's only deleting from one table. My other suspicion is that it's determined when multiple WHERE clauses are used for multiple tables. If you could include examples of both in your answer it would be much appreciated!
EDIT :
I'm asking this question because when performing certain DELETE queries with a LIMIT I get an error that you cannot use LIMIT with multiple-table syntax.
EDIT #2:
In a nutshell, you cannot use ORDER BY or LIMIT if you are joining tables in a DELETE query.
MySQL's documentation states the following
Regarding the "single-table" synatax:
If the ORDER BY clause is specified, the rows are deleted in the order that is specified. The LIMIT clause places a limit on the number of rows that can be deleted.
Regarding the "multi-table" syntax:
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.
Some testing is revealing in the matter of the ORDER BY limitations.
This is a valid single-table DELETE statement with an ORDER BY:
DELETE FROM table
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
A similar query with an explicit joining condition against one other table results in a syntax error on the ORDER BY, despite only one of the tables being targeted for deletion:
DELETE table1
FROM table1 JOIN table2 ON table1.id = table2.id
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
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 'ORDER BY timestamp LIMIT 2' at line 1
Specifying the same query with an implicit join fails the same way
DELETE table1
FROM table1, table2
WHERE
table1.id = table2.id
AND somecol = 'someval'
ORDER BY timestamp LIMIT 2
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 'ORDER BY timestamp LIMIT 2' at line 1
Specifying no table after DELETE for deletion in a joined (multi-table) statement so it looks more like a single-table syntax is also a syntax error
DELETE /* no table named here */
FROM table1 JOIN table2 ON table1.id = table2.id
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
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 'WHERE somecol
It's the table named in the DELETE clause:
Finally, using only one table, but with the mult-table syntax (naming a table after the DELETE keyword) does not permit an ORDER BY, so the true identifying difference here appears to be tables named in the DELETE clause to distinguish multi-table from single-table:
This query only involves one table (without a join), but produces a syntax error:
/* name table1 in the DELETE clause */
DELETE table1
/* but not other table is joined */
FROM table1
WHERE somecol = 'someval'
ORDER BY id LIMIT 2
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 'ORDER BY id LIMIT 1
If you specify that you DELETE from only 1 table and there are no JOINs involved in your DELETE statement, then it's a single-table syntax. Otherwise, it's a multi-table syntax. I am pretty sure that's what the doc page means.
I think a proof for this interpretation is that down there the page says:
The table_references clause lists the tables involved in the join. Its syntax is described in Section 13.2.8.2, "JOIN Syntax".
Note that in the single-table syntax there's no table_references element.

mysql error update or insert failing

I'm going blind here... can't seem to find the error in this SQL:
INSERT INTO sankt_groups_order (
parent_group_id,
child_group_id,
order
) VALUES (?,?,?)
ON DUPLICATE KEY UPDATE
order = ?
;
I am getting this error:
SQLSTATE[42000]: Syntax error or access violation:
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 'order ) VALUES ('65',NULL,'3') ON DUPLICATE KEY UPDATE order = '3''
Next will this SQL do what I think? I need it to insert the whole row if missing and update order if it exists... I have an index making parent_group_id and child_group_id unique.
order is a reserved word in mysql, you'll have to escape it:
child_group_id,
`order`
^-- ^--- backticks to escape
) VALUES (?,?,?)
and yes, it should do what you think. If there's a unique/primary key violation, you'll only change the order field.

MySQL foreign key problem

I'm getting that error running on MySQL 5.5.8
Mysql2::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 'WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
EN' at line 6:
CREATE TRIGGER fk_brands_products_insert
BEFORE INSERT ON brands
FOR EACH ROW BEGIN
SELECT
RAISE(ABORT, "constraint violation: fk_brands_products")
WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
END;
What could be wrong?
I suspect the problem is there is no FROM clause in your select statement.
Are you sure you can raise errors inside a query like that? I can't find it anywhere. I think the proper way would be to select a COUNT or EXISTS and return the result of that INTO a variable. Then, after the query, raise an error if the result doesn't meet your expectations.
Something like this:
SELECT count(id) INTO IDCOUNT FROM products WHERE id = NEW.brand_id;
Wouldn't it be better by the way to just add a real constraint? Or do you use a storage type that doesn't support that?

Modifing select query to delete query

The following select query works fine:
SELECT * FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
However, when I try to delete the rows retrieved here, it fails
DELETE FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
What is wrong here?
The error message is:
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 'job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LI' at line 1
You need to specify you are deleting from the alias table, so use:
DELETE job FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
i have tested the query in sql server. works fine but there is possible that the values you are deleting have some relationship with other table like PK and FK.
if they have then you have to delete the records from those tables too..........