Delete, Update with derived tables?

后端 未结 2 1314
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 05:52

I have just studied FROM clause and derived tables in mysql and most of the websites provided the examples using SELECT command

<
2条回答
  •  时光取名叫无心
    2021-01-20 06:12

    Derived tables exist only for the duration of the parent query they're a member of. Assuming that this syntax and the operations were allowed by MySQL, consider what happens:

    a) Your main query starts executing
    b) the sub-query executes and returns its results as a temporary table
    c) the parent update changes that temporary table
    d) the parent query finishes
    e) temporary tables are cleaned up and deleted

    Essentially you'll have done nothing except waste a bunch of cpu cycles and disk bandwidth.

    UPDATE queries DO allow you to join against other tables to use in the WHERE clause, e.g..

    UPDATE maintable
    LEFT JOIN othertable ON maintable.pk = othertable.fk
    SET maintable.somefield='foo'
    WHERE othertable.otherfield = 'bar'
    

提交回复
热议问题