SQL problems encountered at work: Field a of table A is updated to field b of table B & field c of table A is refreshed based on field a.
1: Requirements introduction
1: The a field of table A is updated to
the value of the name field of the user table of the b field of the B table, and is synchronized to the user_name word of the user_info table.
2: The c field of table A is refreshed based on the a field. The
names field of the demo table stores multiple names, separated by English commas.
2: Update field a of table A to field b of table B
user table
user_info table
## 更新book表中的author字段 变成user表中的name
update user_info ui set user_name = (
select u.name from user u where u.id = ui.user_id
)
// where ui.user_id in (select id from user )
## 这里 where 条件必须检查下id相同的才更新,否则不在user表的数据,user_name字段会被更新为null
Results of the
ps: This batch operation will be very slow for tables with large amounts of data.
Three: The c field of table A is refreshed based on the a field.
The count field of the demo table counts how many names there are in names
Query sql
select *,IF
((
LENGTH( names )- LENGTH(
REPLACE ( names, ',', '' )) + 1
) IS NULL,
0,(
LENGTH( names )- LENGTH(
REPLACE ( names, ',', '' )) + 1
)
)
from demo
search result:
update sql
## 统计每行数据有多少个名字
update demo set count = IF
((
LENGTH( names )- LENGTH(
REPLACE ( names, ',', '' )) + 1
) IS NULL,
0,(
LENGTH( names )- LENGTH(
REPLACE ( names, ',', '' )) + 1
)
) ;
Update result
ps: I only record the SQL problems encountered at work. I am Zha Zhatao, and I have to study hard.