MySQL - UPDATE query based on SELECT Query

后端 未结 11 2061
既然无缘
既然无缘 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:53

    You can use:

    UPDATE Station AS st1, StationOld AS st2
       SET st1.already_used = 1
     WHERE st1.code = st2.code
    
    0 讨论(0)
  • 2020-11-22 00:54
    UPDATE 
      receipt_invoices dest,
      (
        SELECT 
          `receipt_id`,
          CAST((net * 100) / 112 AS DECIMAL (11, 2)) witoutvat 
        FROM
          receipt 
        WHERE CAST((net * 100) / 112 AS DECIMAL (11, 2)) != total 
          AND vat_percentage = 12
      ) src 
    SET
      dest.price = src.witoutvat,
      dest.amount = src.witoutvat 
    WHERE col_tobefixed = 1 
      AND dest.`receipt_id` = src.receipt_id ;
    

    Hope this will help you in a case where you have to match and update between two tables.

    0 讨论(0)
  • 2020-11-22 00:55

    You can update values from another table using inner join like this

    UPDATE [table1_name] AS t1 INNER JOIN [table2_name] AS t2 ON t1.column1_name] = t2.[column1_name] SET t1.[column2_name] = t2.column2_name];
    

    Follow here to know how to use this query http://www.voidtricks.com/mysql-inner-join-update/

    or you can use select as subquery to do this

    UPDATE [table_name] SET [column_name] = (SELECT [column_name] FROM [table_name] WHERE [column_name] = [value]) WHERE [column_name] = [value];
    

    query explained in details here http://www.voidtricks.com/mysql-update-from-select/

    0 讨论(0)
  • 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 (<the above select statement>);
    
    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

    0 讨论(0)
  • 2020-11-22 00:58
    UPDATE [table_name] AS T1,
          (SELECT [column_name] 
            FROM [table_name] 
            WHERE [column_name] = [value]) AS T2 
      SET T1.[column_name]=T2.[column_name] + 1
    WHERE T1.[column_name] = [value];
    
    0 讨论(0)
提交回复
热议问题