日常积累
- insert 与select的结合使用
- insert into 插入的值是select查询出来的结果
格式:
INSERT INTO table1 (c1, c2, c3) (SELECT v1, v2, v3 FROM table2)
栗子:
insert into spider_center.user_authorization (auth_type,user_id) select 0, u.id from spider_center.user u where u.phone_no = '$1';
其中,0是auth_type的值 - update与select的结合使用
- update语句的条件是select出来的值
格式:
UPDATE TABLE_NAME SET column_name1 = VALUE WHRER column_name2 = (select id from XXX)
栗子:
update xyqb.repayment_plan set repayment_status=1 where user_id=(select id from xyqb.user where phone_no = '$1');
- update的value值是select的结果
格式:
UPDATE table1 alias SET (column_name,column_name ) = ( SELECT (column_name, column_name) FROM table2 WHERE column_name = alias.column_name) WHERE column_name = VALUE
栗子:
UPDATE b SET ClientName = a.name FROM a,b WHERE a.id = b.id
如果有以下报错
Truncated incorrect DOUBLE value:
有可能是update同时要修改两个字段的值,两个字段之间不要用and,直接使用逗号连接即可
网友评论