Can I do a mysql Select, Update and Delete in one query?

前端 未结 3 660
悲哀的现实
悲哀的现实 2021-01-05 16:16

Can I say that one of many ways to optimize mysql is to reduce the number of queries?

If that so, can I do this:

- Select \"data\" => $A from tabl         


        
3条回答
  •  执念已碎
    2021-01-05 17:14

    NO,
    only can combine

    • DELETE and SELECT
    • UPDATE and SELECT

    This is not a proper way for mysql optimization simply
    because each query come with different query cost.

    And in myisam, it involve table level locking for write

    Example for UPDATE and SELECT

    /* this will update TABLE_A if ID in TABLE_B exist in TABLE_A */
    UPDATE TABLE_A, TABLE_B
      SET TABLE_A.SOME_COLUMN=TABLE_B.SOME_COLUMN
    WHERE TABLE_A.ID=TABLE_B.ID
    
    /* or */
    UPDATE TABLE_A
      SET SOME_COLUMN = (SELECT SOME_COLUMN_B FROM TABLE_B WHERE ... LIMIT 1)
    

    Example for DELETE and SELECT

    DELETE FROM TABLE_A WHERE TABLE_A IN(SELECT ID FROM TABLE_B)
    

提交回复
热议问题