简体   繁体   中英

how to replace database value by update table php

I have created two tables subscription_pending and subscription_complete

subscription_complete contain

id mag1 mag2 mag3 mag4
4  100  0    100  0

subscription_pending contain

id mag1 mag2 mag3 mag4
4       100    

I have insert value by following command

$final_q=  "INSERT INTO `subscription_complete` (`id`, `mag1`, `mag2`, `mag3`, `mag4`) VALUES ('".$row_q['id']."','".$row_q['mag1']."','".$row_q['mag2']."','".$row_q['mag3']."','".$row_q['mag4']."'

i want to update the subscription_complete table without replacing its value ie 100 for that i hvae write following query

$final_q1= "update subscription_complete set mag1='".$row_q['mag1']."',mag2='".$row_q['mag2']."',mag3='".$row_q['mag3']."',mag4='".$row_q['mag4']."' where id='".$row_q['id']."'"; 

can you tell me how to set 100 value in remaining column where the value is 0 ?

Thank you in advance !!

This assumes the empty fields in subscription_pending contain NULL , not empty strings.

UPDATE subscription_complete AS c
JOIN subscription_pending AS p ON p.id = c.id
SET c.mag1 = IFNULL(p.mag1, c.mag1),
    c.mag2 = IFNULL(p.mag2, c.mag2),
    c.mag3 = IFNULL(p.mag3, c.mag3),
    c.mag4 = IFNULL(p.mag4, c.mag4)

If they're actually empty strings, use IF(p.magX = '', c.magX, p.magX) instead of the IFNULL test.

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