查詢插入(更新 刪除)

2021-09-21 18:07:43 字數 2231 閱讀 3156

查詢插入(更新)可用於刷資料,新建乙個中間表,匯入資料,再把資料刷到目標表上

一、mysql:

1、查詢插入:把乙個資料表查詢的結果插入另乙個表:

insert into t_user_project1 (user_id, project_id,is_delete, create_time,update_time, is_current_project)select

distinct(task.assigned_charge_id),1,0,now(),now(),0

from t_crowd_task_nearest task where

task.is_delete = 0 and assign_type = 0 and task.project_id=1;

insert into t_user_project1 (user_id, project_id,is_delete, create_time,update_time, is_current_project)select

distinct(task.assigned_agent_id),1,0,now(),now(),0

from t_crowd_task_nearest task where

task.is_delete = 0 and assign_type = 1 and task.project_id=1

and task.assigned_agent_id not in (select user_id from t_user_project1 )

insert into t_menu (

resource_code,

resource_name,

resource_value,

parent_resource_id,

resource_type,

vue_url

) select

'usermanager',

'使用者管理',

'url',

id,3, '/user/usermanager'

from

t_menu

where

resource_code = 'user'

2、查詢更新:mysql使用乙個表的資料更新另一張表:

update tb1,tb2 set tb1.address=tb2.address where tb1.name=tb2.name
或連線查詢:

update tb1 a

left join tb2 b on a.name = b.name

left join tb3 c on b.id = c.user_id

set a.address=b.address

where a.is_delete = 0 and b.is_delete = 0;

3、查詢刪除:mysql查詢刪除不支援from後面的查詢語句包含主表名,如選單表

delete  from t_menu where parent_menu_id in (

select id from t_menu where menu_id ='user'

);

會報錯[err] 1093 - you can't specify target table 't_menu' for update in from clause ,可以加個臨時表:

delete  from t_menu where parent_menu_id in (

select e. id from (

select id from t_menu where menu_id ='user'

) e);

二、sqlserver

1、查詢插入:

insert into table_a(account) select user_id from table_b;
2、查詢更新: 

update dbo.table_1 

set dbo.table_1 .project_code=dbo.table_2.project_code

from dbo.table_1 , dbo.table_2  where 

dbo.table_1.project_name=dbo.table_2.project_name;

資料插入更新刪除

insert into 表名 values 值1,值2 insert into student id,name,age,email value 1 張三 18 男 zhangsan 163.com insert into 表名 欄位名1 欄位名2 value 值1,值2 insert into 表名...

資料 更新 插入 刪除

1.update實現對資料的更新操作,語法如下 更新單行資料 update dbtab set f1 g1 fn gn where f表組建欄位名,g為新設定的值,where為確保只更新單行。注 除f g外還可 f f g f f g 通過工作區更改單行資料 update dbtab from wa...

jdbc運算元據庫插入 更新 刪除 查詢

mysql資料庫 public boolean insertfrom person per catch sqlexception e finally return flag public boolean updatefrom int id,int string name catch sqlexcep...