简体   繁体   English

如何通过更新表php替换数据库值

[英]how to replace database value by update table php

I have created two tables subscription_pending and subscription_complete 我创建了两个表subscription_pending和subscription_complete

subscription_complete contain subscription_complete包含

id mag1 mag2 mag3 mag4
4  100  0    100  0

subscription_pending contain subscription_pending包含

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 我想更新subscription_complete表而不替换其值,即我要在其中写以下查询的100

$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 ? 您能告诉我如何在剩余列中将值设为0的情况下设置100值吗?

Thank you in advance !! 先感谢您 !!

This assumes the empty fields in subscription_pending contain NULL , not empty strings. 假定subscription_pending的空字段包含NULL ,而不是空字符串。

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. 如果它们实际上是空字符串,请使用IF(p.magX = '', c.magX, p.magX)代替IFNULL测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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