注意insert into select 没有valus ,没有括号
mysql
insert into zforce.shop_all (shop_name,address,city_id,market_circle_id,cat_id) select shop_name,shop_address,city_id,market_circle_id,business_scope from backend.franchise_tore_info where create_time >='2014-09-25 18:33:13'
SELECT INTO 的用法是 select val1,valu2 into tab2 from tab1。要求tab2不存在,插入时会自动建立tab2。sql
在mysql中不支持这种用法数据库
能够使用 create table temp select * from dept;mybatis
若是只是须要表结构 create table temp select * from dept where 1=2;spa
若是须要建立临时表,能够加关键字 TEMPORARY 。在某些状况下使用临时表,能够加快查询效率,好比某个查询的一个子查询会执行屡次。能够使用临时表。code
create TEMPORARY table temp select * from dept;ci
这样数据库在该链接关闭的时候,自动删除该表。这意味着两个不一样的链接能够使用相同的临时表名称,同时两个临时表不会互相冲突,也不与原有的同名的非临时表冲突。it
建立这种临时表,必须拥有CREATE TEMPORARY TABLES权限.table
须要注意的是这种临时表不能应用到子查询class
使用以下方式,能够对插入进行判断
insert into zforce.shop_all(city_id,shop_name,address,gmt_create,deleted) select 3,'xxx','xxx',now(),0 from dual where not EXISTS (SELECT 1 FROM zforce.shop_all zs where city_id = 3 and zs.shop_name = 'xxx' and zs.address = 'xxx')
更新表时,若是须要根据条件判断须要更新的值,能够用case when。由于mybatis中的关键字冲突,须要<![CDATA[ ]]>
<![CDATA[ update zforce.contract zc set zc.contract_status = (case when now()<contract_start_time then 1 when now()>=contract_start_time and now()<= DATE_SUB(contract_start_time, INTERVAL 14 DAY) then 2 when now()>DATE_SUB(contract_start_time, INTERVAL 14 DAY) and now()<contract_end_time then 3 when now()>contract_start_time then 4 end) ]]>