Update column in one table based on value in another table in mysql

Sanju Menon

I have a table called 'quot_items', that holds a grand_total field . It has this structure

tender_id |  grand_total
15001          100000
15002          250000
15003          1500000
16009          4500000

I have another table called 'quotation' which holds a 'category' field. It has this structure.

tender_id |  category
15001          H
15002          E
15003          B
16009          A

What iam trying is i need an UPDATE statement in MYSQL where in there are certain conditions:

If [grand_total'] < '100000') then Category H ( which means if grand_total value of table 'quot_items' is < 100000 then Update table 'quotation' category field as 'H'.

If (['grand_total'] >= '100000') && (['grand_total'] <= '200000') then Category 'G'.

If (['grand_total'] > '200000') && (['grand_total'] <= '600000') then Category 'F'.

If (['grand_total'] > '600000') && (['grand_total'] <= '1000000') then Category 'E'.

If (['grand_total'] > '1000000') && (['grand_total'] <= '1500000') then Category 'D'.

There are more conditions. I need a query to do this in MYSQL so that i can UPDATE my DB in one update statement. Pls anyone can help me.

I have tried the following:

UPDATE quotation INNER JOIN quotation_items ON quotation.tender_id = quotation_items.tender_id
SET quotation.category = (
    CASE WHEN quotation_items.grand_total < 100000 then 'H' 
    WHEN quotation_items.grand_total >= 100000 && quotation_items.grand_total <= 200000 then 'G'
    WHEN quotation_items.grand_total > 200000 && quotation_items.grand_total <= 600000 then 'F'
    WHEN quotation_items.grand_total > 600000 && quotation_items.grand_total <= 1000000 then 'E'
    WHEN quotation_items.grand_total > 1000000 && quotation_items.grand_total <= 1500000 then 'D'
    END
);
Blank

Try this:

UPDATE quotation t1
INNER JOIN quot_items t2
ON t1.tender_id = t2.tender_id
SET t1.category = 
    CASE WHEN t2.grand_total < 100000 THEN 'H' 
    WHEN grand_total >= 100000 AND grand_total <= 200000 THEN 'G'
    WHEN grand_total > 200000 AND grand_total <= 600000 THEN 'F'
    WHEN grand_total > 600000 AND grand_total <= 1000000 THEN 'E'
    WHEN grand_total > 1000000 AND grand_total <= 1500000 THEN 'D'
    ELSE t1.category
    END

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update column in one table based on value in another table in mysql

From Dev

Update value in a column based on another column in the same table in MYSQL

From Dev

Update value in column based on column count in another table with mysql

From Dev

mysql update column value from another table

From Dev

update column in one table based on condition in another table

From Dev

MySQL - Update/Set a column in one table equal to MAX value from another table

From Dev

Update mysql one table from another table based on dates

From Dev

Update multiple columns in one table based on values in another table in mysql

From Dev

MySQL how to UPDATE a field in one table based on values in another table

From Dev

Update a column in a table based on column in another table

From Dev

Mysql - update table column from another column based on order

From Dev

How to update mysql column with another value from another table?

From Dev

How to update mysql column with another value from another table?

From Dev

How to update table based on another table column?

From Dev

Select COLUMN name from one table based on VALUE from another in mySQL?

From Dev

Update table with another table based on cell value

From Dev

count one column and update another in same table in mysql

From Dev

update one table depend on the value of another table

From Dev

Update column from another table column value

From Dev

Update a column of NAs in one data table with the value from a column in another data table

From Dev

MYSQL query - simple way to return all values from one column based on a DISTINCT value in another column in the same table?

From Dev

SQL Server query to update one table column value with another table value

From Dev

Update MySQL table taking the value of a column, adding it to another column, the changing the value of the first column

From Dev

Update two columns values in a table based on another column value in different table

From Dev

MySQL query to find if a value of one column in one table is between two values in two columns on another table

From Dev

Update one table based on values in another table using case statement (MYSQL)

From Dev

Trigger to update table based on another table update in mysql

From Dev

update table based on another tables value in sqLite

From Dev

mysql update and insert statements based on another table

Related Related

  1. 1

    Update column in one table based on value in another table in mysql

  2. 2

    Update value in a column based on another column in the same table in MYSQL

  3. 3

    Update value in column based on column count in another table with mysql

  4. 4

    mysql update column value from another table

  5. 5

    update column in one table based on condition in another table

  6. 6

    MySQL - Update/Set a column in one table equal to MAX value from another table

  7. 7

    Update mysql one table from another table based on dates

  8. 8

    Update multiple columns in one table based on values in another table in mysql

  9. 9

    MySQL how to UPDATE a field in one table based on values in another table

  10. 10

    Update a column in a table based on column in another table

  11. 11

    Mysql - update table column from another column based on order

  12. 12

    How to update mysql column with another value from another table?

  13. 13

    How to update mysql column with another value from another table?

  14. 14

    How to update table based on another table column?

  15. 15

    Select COLUMN name from one table based on VALUE from another in mySQL?

  16. 16

    Update table with another table based on cell value

  17. 17

    count one column and update another in same table in mysql

  18. 18

    update one table depend on the value of another table

  19. 19

    Update column from another table column value

  20. 20

    Update a column of NAs in one data table with the value from a column in another data table

  21. 21

    MYSQL query - simple way to return all values from one column based on a DISTINCT value in another column in the same table?

  22. 22

    SQL Server query to update one table column value with another table value

  23. 23

    Update MySQL table taking the value of a column, adding it to another column, the changing the value of the first column

  24. 24

    Update two columns values in a table based on another column value in different table

  25. 25

    MySQL query to find if a value of one column in one table is between two values in two columns on another table

  26. 26

    Update one table based on values in another table using case statement (MYSQL)

  27. 27

    Trigger to update table based on another table update in mysql

  28. 28

    update table based on another tables value in sqLite

  29. 29

    mysql update and insert statements based on another table

HotTag

Archive