1.left函数
left(字段,长度):获取指定字段左侧的数据,类似substring函数
2.union / union all
将多个记录合并成一个完整的数据集
3.insert into select
使用selsect语句来代替values语句,需要保证查询出来的记录为单列单条
Insert into Table2(field1,field2,...) select value1,value2,... from Table1
4.select into from
将查询的数据存入另一张表中
SELECT vale1, value2 into Table2 from Table1
5.多表级联删除
delete from t1 where a=b--常规写法 delete t1 from t2 where a=b--条件成立时删除t1中的数据 delete t1,t2 from t1,t2 where a=b--条件成立时删除t1、t2中的数据
6.查询选修了所有课程的学生信息
select student.id,student.name from student where not exist ( select coID,coName from course not in select coID,coName from takeCourse where studentID=student.id )
7.跨服务器跨库join
使用链接表方式 1.本地的表结构必须与远程的完全一样 2.远程数据库目前仅限MySQL 3.不支持事务 4.不支持表结构修改 CREATE TABLE `user_info` ( `userid` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户id', `username` varchar(60) NOT NULL DEFAULT '' COMMENT '帐号', `level` int(11) NOT NULL DEFAULT '1' COMMENT '等级', `log_dt` timestamp NOT NULL DEFAULT '2016-11-30 00:00:00' COMMENT '最后登录时间', `reg_dt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '注册时间', PRIMARY KEY (`userid`), KEY `idx_name` (`username`) ) ENGINE=FEDERATED DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT CONNECTION='mysql://batsing:b8a7t4@localhost:3306/main_db/user_info' COMMENT='用户基本信息表-链接表';
8.replace into
replace into t(id, update_time) replace into 跟 insert 功能类似,不同点在于:replace into 首先尝试插入数据到表中,
1. 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删除此行数据,然后插入新的数据。
2. 否则,直接插入新数据。