简体   繁体   中英

Updating selected table records using another table (PHP)

Hope someone can assist...

I have two tables containing fish catches and recording the personal bests of each angler. What I have now done is added more species to the ever growing list.

So I have now backed up the old database (catches_old) and created a new table (catches).

Catches has all the species set to zero.

The only way to pull a unique record is using FishName and Angler together.

What I need is a simple piece of code to check if the weight is more than zero, meaning it has a record, then I would update the record with the old details.

I've tried multiple ways but failing miserably.

Table Old (Specie, FishNm, Position, Fisherman, lb, oz, dr, Drams, PegName, Date) Table New is the same.

Table Old contains - Old records from catches. Table new is basically a reset of all records with additional fish names to catch.

Table New contains all fish names and reset to 0 drams.

Table Old now needs to update Table New with the catch records of past catches. Catches that were still not caught in the old table, need not be transferred, just the old catches.

SAMPLE DATA - (not accurate drams calculations lol)

Carp - Wobbles - 2 - David Williams - 12 - 4 - 6 - 897 - Willow Island - 21/05/1999  
Carp - Fanta - 1 - David Williams - 14 - 7 - 9 - 900 - Bay of Dreams - 14/05/2004  
Goldfish - Harry - 1 - Fred Ball - 1 - 1- 2 - 258 - Willow Island - 12/05/2010  
Goldfish - Whipper - 2 - - 0 - 0- 0- 0- -
Goldfish - Harriet - 3 - David Williams - 0 - 15 - 2 - 251 - The Fair - 23/09/2012

New Code - Using an inner join for two unique fields. Should this work ok as it is failing?

UPDATE NewTable  
INNER JOIN OldTable  
    ON NewTable.Fishname = OldTable.Fishname AND NewTable.Angler = OldTable.Angler
SET NewTable.Weight = OldTable.Weight

If you're wanting to take the data from the old, and insert it into the new one, use an insert query with a select statement. It will insert all matching records. You don't necessarily have to use php, just use the mysql query:

INSERT INTO NewTableName (Field1, Field2, Field3, Weight)
SELECT Field1, Field2, Field3, Weight)
FROM OldTableName
WHERE Weight > 0

Note that the order of the fields MUST MATCH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM