I know this argument has been addressed in this site many times, I just can't find a working solution, so I thought i would post my specific case to you, maybe you can help me...

I have a festival from which I need to eliminate the songs that got less than the 20% of the average evaluation of the night before.

I tried with this:

DELETE FROM concorre AS C 
WHERE C.dataSer='2014-02-24' 
AND C.votoTot<(SELECT AVG(B.votoTot) 
               FROM concorre AS B 
               WHERE B.dataSer='2014-02-23')/5 

It resolves the "same table problem" but still gives me 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 'AS C WHERE C.dataSer='2014-02-24' AND C.votoTot<(SELECT AVG(votoTot) FROM concor' at line 1

有帮助吗?

解决方案

Try this

DELETE c FROM concorre C 
JOIN (SELECT AVG(B.votoTot) average FROM concorre B WHERE B.dataSer='2014-02-23') d
WHERE C.dataSer='2014-02-24' 
AND C.votoTot < d.average /5 

其他提示

MySQL does not allow a table that is being updated/deleted in the outer query to be selected from in any sub-query of the statement.

Here you are deleting from concorre table and also referring the same table in sub-query.

You can try something like this : MySQL DELETE FROM with subquery as condition

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top