MySQL - UPDATE query based on SELECT Query

后端 未结 11 2065
既然无缘
既然无缘 2020-11-21 23:58

I need to check (from the same table) if there is an association between two events based on date-time.

One set of data will contain the ending date-time of certain

11条回答
  •  抹茶落季
    2020-11-22 00:56

    I found this question in looking for my own solution to a very complex join. This is an alternative solution, to a more complex version of the problem, which I thought might be useful.

    I needed to populate the product_id field in the activities table, where activities are numbered in a unit, and units are numbered in a level (identified using a string ??N), such that one can identify activities using an SKU ie L1U1A1. Those SKUs are then stored in a different table.

    I identified the following to get a list of activity_id vs product_id:-

    SELECT a.activity_id, w.product_id 
      FROM activities a 
      JOIN units USING(unit_id) 
      JOIN product_types USING(product_type_id) 
      JOIN web_products w 
        ON sku=CONCAT('L',SUBSTR(product_type_code,3), 'U',unit_index, 'A',activity_index)
    

    I found that that was too complex to incorporate into a SELECT within mysql, so I created a temporary table, and joined that with the update statement:-

    CREATE TEMPORARY TABLE activity_product_ids AS ();
    
    UPDATE activities a
      JOIN activity_product_ids b
        ON a.activity_id=b.activity_id 
      SET a.product_id=b.product_id;
    

    I hope someone finds this useful

提交回复
热议问题