简体   繁体   English

php mysql只更新空列

[英]Php mysql to update only empty column

Consider I have 4 columns in a table and i have datas for 3 columns like below 考虑我在表中有4列,我有3列的数据,如下所示

TableTest

Col1  | Col2  | Col3
D11   | D12   |   
D21   | D22   | 

Normally the update query would be 通常更新查询将是

Update TableTest SET Col1 = D11 , Col2 = D12 , COL3 = newdata Where Col1= D11

The Scenario is , the update query should only push data to the COL3 , it should skip the Col1 and Col2, as it has already filled with data(even if same or different data for the Col1 and Col2) 方案是,更新查询应该只将数据推送到COL3,它应该跳过Col1和Col2,因为它已经填充了数据(即使Col1和Col2的数据相同或不同)

This might help - 这可能有所帮助 -

UPDATE TableTest a
INNER JOIN TableTest b ON a.Col1 = b.Col1
SET a.Col3 = 'newData'
WHERE a.Col3 IS NULL

An INNER JOIN with the same table so that it updates the appropriate row! INNER JOIN具有相同的表,以便更新相应的行!

If you just want to update COL3 than don't include other columns in UPDATE query. 如果您只想更新COL3而不是在UPDATE查询中包含其他列。

Query: 查询:

Update TableTest SET COL3 = newdata Where Col1= D11

You should update whole table using single query as: 您应该使用单个查询更新整个表

Update TableSet SET COL3=CONCAT('D',CONVERT(Substr(Col2,2),INT)+1)

This will update table as follows: 这将更新表如下:

TableTest

Col1  | Col2  | Col3
D11   | D12   |  D13 
D21   | D22   |  D23

就这样做,像这样:

Update TableTest SET  COL3 = newdata Where Col1= D11

In UPDATE query, there is no need to reassign the value of col1 and col2 if those values are not changing. UPDATE查询中,如果这些值未更改,则无需重新分配col1col2的值。

UPDATE TableTest 
SET COL3 = newdata 
WHERE Col1= 'D11';

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

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