dames
dames

Reputation: 1481

copying a cell from one table to another in mysql

How do you go about copying a cell from one table to another using mysql and php reason i asked is I have two tables payment and payment history, payment stores the values of the last payment and shows what is currently owed however payment history stores all the payment transactions made and the amount owed after each transaction therefore the main reason i need it is to show the various amounts owed in respect to the payments, so for payment table i would use an update query and for payment history I use an insert query

Upvotes: 1

Views: 920

Answers (3)

Nico
Nico

Reputation: 473

You can do this with a trigger, just update the table1 and table2 will get the new record.

You'll have to do something like this:

CREATE TRIGGER triggername AFTER INSERT ON table1
  FOR EACH ROW BEGIN
    INSERT INTO table2 (column1, column2 ) VALUES (OLD.value1, OLD.value2);
  END;

Upvotes: 0

kasavbere
kasavbere

Reputation: 6003

if you need to insert

insert into myTable (field1, field2) 
select from myotherTable where somefield = "something";

if you need to simply update

update myTable set field1 = 
(select field1Equivalent from otherTAble where someField="something");

Upvotes: 0

GavinCattell
GavinCattell

Reputation: 3963

If you want to INSERT into a table from another table you can use:

INSERT INTO TableBar (column1, column2 )
SELECT TableFoo.column1, TableFoo.column2 
FROM TableFoo
WHERE <<YOUR CLAUSE HERE>>

Upvotes: 2

Related Questions