search

Home  >  Q&A  >  body text

I'm trying to compare values ​​in two columns in two different tables

I have two tables, tableA and tableB. Table A has 4 columns, while Table B has 3 columns. The last column of table A is blank and in this column I want to show the difference between the third column in tables A and B.

Table A name=valuation_average. Table B Name = Valuation_Cost. Their third column names are both cost_col (but the values ​​are different, I want to show this difference in the fourth column in tableA, called "difference", the column already exists and named it just has no value.)

Also remember that the rows may not be equal, I mean the first row in table A may be different from the first row in table B. The primary key is the first column of both tables, called "code"

P粉311423594P粉311423594257 days ago443

reply all(1)I'll reply

  • P粉764003519

    P粉7640035192024-04-05 10:22:25

    You can try mysql-update-join.

    In your case, maybe

    UPDATE valuation_average a,valuation_cost c SET a.difference = a.cost_col - c.cost_col WHERE a.code = c.code

    can solve your problem.

    reply
    0
  • Cancelreply