3

Possible Duplicate:
Mysql error 1093 - Can’t specify target table for update in FROM clause

I am getting an error when putting SELECT query in WHERE clause of UPDATE query.

my Query is like this :

UPDATE `subschedulesseats` m
SET m.studentid='1'
WHERE m.`seatid`= (
    SELECT h.`seatid`
    FROM `subschedulesseats` h
    WHERE h.`sessiontime`='02:30~04:00'
    ORDER BY h.`seatid` ASC
    LIMIT 2,1
)

AND Error will be shown is like this :

"You can't specify target table 'm' for update in FROM clause"

I have attached a snap shot of the error display.

enter image description here

Please anyone can help me in this problem?

Thank You in Advance

Community
  • 1
  • 1
Hardik
  • 1,429
  • 2
  • 19
  • 37

2 Answers2

7

Actually you can update it by wrapping it in a subquery (thus creating temporary table for the result)

UPDATE `subschedulesseats` m
SET m.studentid='1'
WHERE m.`seatid`= 
(
    SELECT seatID
    FROM
    (
        SELECT h.`seatid`
        FROM `subschedulesseats` h
        WHERE h.`sessiontime`='02:30~04:00'
        ORDER BY h.`seatid` ASC
        LIMIT 2,1
    ) s
)

or by using JOIN

UPDATE  `subschedulesseats` m
        INNER JOIN
        (
            SELECT seatID
            FROM
            (
                SELECT h.`seatid`
                FROM `subschedulesseats` h
                WHERE h.`sessiontime`='02:30~04:00'
                ORDER BY h.`seatid` ASC
                LIMIT 2,1
            ) s
        ) t ON m.seatID = t.seatID
SET     m.studentid = '1'
John Woo
  • 258,903
  • 69
  • 498
  • 492
2

In MySQL, you can't modify the same table which you use in the SELECT part. This behaviour is documented at: http://dev.mysql.com/doc/refman/5.6/en/update.html

The reference

https://stackoverflow.com/a/45498/1225190

Community
  • 1
  • 1
Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
  • You mean to say that We can't update the same table using select query? there is not a way to achieve this using single query – Hardik Nov 08 '12 at 06:56
  • Please refer this answer dude... http://stackoverflow.com/a/45498/1225190 . this is a duplicate question – Miqdad Ali Nov 08 '12 at 06:57