简体   繁体   中英

How do i update 1 field or 2 of a database table if another table has matching field values?

Let's say I have a table with these columns and values:

id    field
-----------
1     0001
2     0002
3     0003
4     0004
5     0005

Then i have this other table with these fields and values:

id    tid    info_1    field_1    info_2    field_2
---------------------------------------------------
1     1      7         0000       null      null
2     2      null      null       10        0002
3     3      17        0003       null      null
4     4      29        1111       null      null
5     6      null      null       44        0005

I need an update query that will update the records of table 2, just in the info columns, but only if table 1 has matching fields in table 2 and also only when id of first table and tid of second table match.

So for example table 1 id of 1 and table 2 tid of 1 match but field_1 or field_2 doesn't have the same field number as table 1.

Table 1 id of 2 and table 2 tid of 2 match and field_2 does have a matching number as the field in table one, so i would like to change info_2 of record 2 of table 2 to a number of my choosing. let's just say 50. in all cases the number in info_1 or info_2 will be changed to 50.

Now table 1 id of 3 and table 2 tid of 3 match and field_1 has matching number as the field in table 1, so i need to change info_1 to 50 for that 3rd record in table 2.

In record 4 the id and tid match but no fields match so skip that one.

In record 5 even though field_2 of table 2 matches field in table one, the id of first table and tid of second table do not match, so that 5th record can be skipped.

Here is what i tried so far:

$updatequery1 = "UPDATE table2 LEFT JOIN table1 a ON a.field = field_1 SET info_1 = 50";
$updateresult1 = mysql_query($updatequery1) or die(mysql_error());

So for this first attempt i didn't set id and tid to match, i just went right for looking for field matching. i also didn't incorporate field_2. not even sure if it can all be done in one pass or if multiple queries are needed. in any case, this attempt made all info_1 values equal to 50 even if there were no field matches.

Can someone help me with this query and make both info columns change at same time if possible?

If i were to do a select query for selecting all records that match in table 1 id and table 2 tid, it would be like this, but don't know how to incorporate it into the update query.

SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2
ON table1.id = table2.tid

I can also try and incorporate field matching and it would probably be something like this:

SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2
ON table1.id = table2.tid
WHERE table1.field = table2.field_1
OR table1.field = table2.field_2

But again, wouldn't know how to turn that into an update query so that either info_1 or info_2 changes accordingly.

So to summarize in a quicker way. if id and tid match and field and field_1 match, info_1 should change to 50. if id and tid match and field and field_2 match, info_2 should change to 50

you can use CASE statement in the SET

UPDATE Table2 as T2
JOIN Table1 T1
ON T2.tid = T1.id
set 
info_1 = CASE WHEN T2.field_1 = T1.field then 50 ELSE T2.field_1 END  ,
info_2 = CASE WHEN T2.field_2 = T1.field then 50 ELSE T2.field_2 END 

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