Error Code: 1093. You can't specify target table 'c' for update in FROM clause

Cristian Flaviu :

I'm trying to prepare myself for feature interviews and while I was trying to solve this problem I have encountered some issues at the last exercise C).

enter image description here

I have recreated these tables on my local database "labsd" and this is my solution to

A)

SELECT  labsd.interns.email,labsd.grades.grade
from labsd.grades
Inner join labsd.interns on labsd.interns.id=labsd.grades.id
Order by labsd.grades.grade desc
Limit 10;

B)

SELECT count(labsd.grades.id),labsd.grades.grade
from grades
group by labsd.grades.grade
order by count(labsd.grades.id) desc;

And here is the problem

C)

Update labsd.interns as c
Set c.Accepted=1
where labsd.interns.id In (SELECT  labsd.interns.id
from labsd.grades
Inner join labsd.interns on labsd.interns.id=labsd.grades.id
Order by labsd.grades.grade desc);

Considering that at the first point I was asked for the top 10 students I thought I should reuse that code but I can not use Limit anymore and I got the following error :

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

I don't know how to find what version of MySql do I have just installed it this semester so I assume it is the last version.

And even without using the limit I still get the error mentioned in the title. I have tried to rename the table but it hasn't solved my problem.

GMB :

MySQL does not allow the syntax that you are trying to use. Instead, you can use the update/join syntax as follows:

update labsd.interns s
inner join (
    select i.id
    from labsd.grades g
    inner join labsd.interns i on i.id = g.id
    order by g.grade desc limit 10
) t on t.id = s.id
set s.accepted = 1

The subquery enumerates the ids of the 10 best candidates, then the outer query joins the results of the subquery with the original table, an sets the flags on matching rows.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=376111&siteId=1