0

I have a problem with this query:

DELETE FROM tableA
WHERE idTableA IN
(SELECT tA.idTableA
    FROM tableB tB
    LEFT JOIN tableA tA ON tB.idTableB = tA.idTableA
    WHERE tB.campoX = 100 LIMIT 1);

Error response: Error Code: 1235. This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

EXAMPLE DATA:

TableB 10 rows (it's a table master)

TableA 50 rows (it's a table of details of tableB)

I would like to delete all records detail of tableA related to master (tableB) with condition filter on tableB and limited for 1 row ( this limit is only to measure the execution time for one delete row)

version of MySql: 8

some idea? tk

GMB
  • 216,147
  • 25
  • 84
  • 135
  • Idea: how about you give us more details? Like the MySQL version/setup you're using that this error obviously mentions – t1f Mar 27 '20 at 11:00
  • Does this answer your question? [MySQL - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery](https://stackoverflow.com/questions/17892762/mysql-this-version-of-mysql-doesnt-yet-support-limit-in-all-any-some-subqu) – P.Salmon Mar 27 '20 at 11:02
  • `LIMIT` without `ORDER BY` makes no sense. What is the logic for choosing just one record from the subquery? – Tim Biegeleisen Mar 27 '20 at 11:03
  • 'this limit is only to measure the execution time for one delete row' - will give a completely meaningless result. – P.Salmon Mar 27 '20 at 11:15
  • @P.Salmon why? it is not real? – Gianfranco Fangano Mar 27 '20 at 11:20
  • @GianfrancoFangano, yes it is not real. Or at least not guaranteed to be representative. Finding the first element might be almost free, but scanning the whole set might be pretty involving. The opposite could also be true. Your initial overhead might be 99% of the cost... – Dr Phil Mar 27 '20 at 12:20

3 Answers3

0
DELETE tableA.*
FROM tableA 
JOIN (SELECT tA.idTableA
      FROM tableB tB
      LEFT JOIN tableA tA ON tB.idTableB = tA.idTableA
      WHERE tB.campoX = 100 LIMIT 1) USING (idTableA);

?

Akina
  • 39,301
  • 5
  • 14
  • 25
0

You can use exists to select the rows (this best handles the case when several rows match in b), and use limit in the outer query directly:

delete from tablea a
where exists (select 1 from tableb b where b.campox = 100 and b.idtableb = a.itablea)
order by a.idtablea
limit 1

Note that I added an order by clause to the query, so the record to delete is clearly defined.

GMB
  • 216,147
  • 25
  • 84
  • 135
0

You can use an alias on subquery and it's easy that way rather than joining it. Here's how it can be done as well

DELETE FROM tableA
WHERE idTableA IN
(select x.idTableA from (
SELECT tA.idTableA
    FROM tableB tB
    LEFT JOIN tableA tA ON tB.idTableB = tA.idTableA
    WHERE tB.campoX = 100 LIMIT 1
) as x);
Koushik Das
  • 9,678
  • 3
  • 51
  • 50