整理了日常用到的一些sqls

1.插入表

insert into table_B select * from table_A

2.清空表

truncate table test #清空表,结构还存在
delete table test where ... #删除表中特定的数据
drop table test #删除整表,包括结构

3. 导出到文件

select * from table_A where filed1 like '8422%' into outfile '/usr/local/mysql/8422.txt' fields terminated by '||'

4.导入文件到DB

LOAD DATA INFILE "/root/user/output.txt" INTO TABLE table_A CHARACTER SET gbk fields terminated by x'' IGNORE 1 LINES;
#字符集有时添加后会报错,可以去掉

5. 更新表

#一对一的情况
update table_A a, table_B b set a.field1 = b.field1 where a.field2 = b.field2 #一对多的情况
update table_A a inner join table_B b on a.route_id = b.valid_route_id set a.valid_route_id=b.valid_route_id

6. 字段拼接

UPDATE table_A set SIC_DESC=concat_ws('|',SICCODE1,SICCODE2,SICCODE3,SICCODE4)

7.字段截取

create table table_A ENGINE=MyISAM select locname,substring(file_name,1,3) from table_B where LOCNAME !=''

8.删除某列

alter table table_A drop column source_path

9.去重

table_A 
+----+--------+
| id | name |
+----+--------+
| 1 | google |
| 2 | yahoo |
| 3 | msn |
| 4 | google |
| 5 | google |
| 6 | yahoo |
+----+--------+

#1 通过条件选取最大或最小的记录,来达到去重的目的
select * from table_A a where id=(select min(id) from table_A where name=a.name) #2 通过group by 和order by
select * from table_A group by name order by id desc; #3 keep the row with the lowest id
(1)DELETE n1 FROM table_A n1, table_A n2 WHERE n1.id > n2.id AND n1.name = n2.name
(2)DELETEFROM NAMES
WHERE id NOTIN(SELECT*FROM(SELECT MIN(n.id)FROM NAMES n
GROUPBY n.name) x) #4 keep the row with the highest id
DELETE n1 FROM names n1, names n2 WHERE n1.id < n2.id AND n1.name = n2.name

10.分组取最大的N条记录

#先添加一个rowid
select *from sh_20110401_bankup as T where 3>(select count(*) from sh_20110401_bankup
where field1=T.field1 and rowid>T.rowid order by T.field1,T.rowid desc )

11.分组取最大值

#通过两步
(1)create table sh_20110301_0331_ptp_t select record_date,substring(time_stamp,1,2)as tim,count(*)as cc from sh_20110301_0331_ptp
group by substring(time_stamp,1,2),record_date order by record_date,substring(time_stamp,1,2) (2)select * from sh_20110301_0331_ptp_t where cc in (select max(cc) from sh_20110301_0331_ptp_t
group by tim) order by tim

12.拆分分隔符

update table_A set custom11=substring_index(custom11,';',1)

13 日期转数字

update table_A set convert_date=unix_timestamp(record_date)
tt=TIME_TO_SEC(time_stamp)

14 MD5,字段不能有null

update table_A set dd = MD5(CONCAT_ws('$',id,attribute_key,attribute_value)

15 上一个月的数据

select * from review where period_diff(date_format(now() , '%Y%m') , date_format(`Review_Date`, '%Y%m')) =1;

16 上一周的数据

select Rating,count(*) from review where week(`Review_Date`)=7  group by Rating

17 最近7天的数据

select week(`Review_Date`) from review where date_sub(curdate(), INTERVAL 7 DAY) <= date(`Review_Date`) 

18 字符变日期

update table_A set tt= str_to_date(date_created,'%m/%d/%Y')

19 字段拆分

update table_A set custom=substring_index(custom,';',1)

常用SQL整理的更多相关文章

  1. MySQL常用SQL整理

    MySQL常用SQL整理 一.DDL #创建数据库 CREATE DATABASE IF NOT EXISTS product DEFAULT CHARSET utf8 COLLATE utf8_ge ...

  2. DB2和MySQL常用SQL整理

    1.Truncate删除表中所有数据 truncate table USER immediate; 说明:Truncate是一个能够快速清空资料表内所有资料的SQL语法.并且能针对具有自动递增值的字段 ...

  3. Oracle 常用sql整理

    1. 查看当前正在只用的undo段 select s.sid, s.serial#, s.username, r.name, t.STATUS, t.START_TIME, t.USED_UBLK, ...

  4. oracle下关于table的常用sql整理

    创建表,create TABLE table( 列名称1 数据类型1, 列名称2 数据类型2, 列名称3 数据类型3, ......); eg: create table TABLE_24751( i ...

  5. 常用sql语句整理:mysql

    ## 常用sql语句整理:mysql1. 增- 增加一张表```CREATE TABLE `table_name`(  ...  )ENGINE=InnoDB DEFAULT CHARSET=utf8 ...

  6. MSSQL注入常用SQL语句整理

    很多情况下使用工具对mssql注入并不完善,所以我们就需要手工注入,一下是本人收集的一些mssql的sql语句. 手工MSSQL注入常用SQL语句 and exists (select * from ...

  7. mysql整理-常用sql语句

    一.常用sql show variables like 'character_set_client';#查询字符集 show databases;#列出所有的服务器上的数据库alter create ...

  8. oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数

        花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用ora ...

  9. 工作经常使用的SQL整理,实战篇(一)

    原文:工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实 ...

随机推荐

  1. Matlab多个Figure图合成一个Fig

    案例:之前跑过的程序 已经生成了多个matlab图,现在需要进行合并到一个图中. 解决方案,利用图像句柄把figure图像中的参数读入到内存中,然后重新subplot绘制. 程序如下: clc;cle ...

  2. ubuntu下设置环境变量的三种方法【转】

    转自:http://blog.chinaunix.net/uid-26916352-id-3208366.html 通常设置环境变量有三种方法: 一.临时设置 export PATH=/home/ya ...

  3. YTU 2605: 熟悉题型——自由设计(比较大小-类模板)

    2605: 熟悉题型--自由设计(比较大小-类模板) 时间限制: 1 Sec  内存限制: 128 MB 提交: 125  解决: 107 题目描述 声明一个类模板,利用它分别实现两个整数.浮点数和字 ...

  4. 每天一个小算法(matlab armijo)

    下面是 armijo线搜索+最速下降法的小程序,matlab用的很不熟,费了不少劲. 函数: function g=fun_obj(x) syms a b f = 1/2*a^2+b^2-a*b-2* ...

  5. Machine Learning for hackers读书笔记(四)排序:智能收件箱

    #数据集来源http://spamassassin.apache.org/publiccorpus/ #加载数据 library(tm)library(ggplot2)data.path<-'F ...

  6. HDU 2126 (背包方法数) Buy the souvenirs

    DP还有很长很长一段路要走.. 题意:给出n纪念品的价格和钱数m,问最多能买多少件纪念品和买这些数量的纪念品的方案数. 首先,求能买最多的纪念品的数量,用贪心法可以解决.将价钱排序,然后从最便宜的开始 ...

  7. codeforces 430 A Points and Segments (easy)

    题意:给出n个点,m个区间,需要给这些点涂上蓝色或者红色,使得每个区间里面的点的红色的点的个数与蓝色的点的个数的差值小于1 唉,题目的标题就标注了一个easy= = 最开始做的时候对点还有区间都排序了 ...

  8. Postgresql两表联结更新

    Postgresql两表联合更新近日使用Postgresql感到有点不好用,一个联合更新非要这样写语法才对:update d_routetripset name=b.name ,    descrip ...

  9. (转)iOS动画Core Animation

    文章转载:http://blog.sina.com.cn/s/blog_7b9d64af0101b8nh.html 在iOS中动画实现技术主要是:Core Animation. Core Animat ...

  10. HDU 5264 pog loves szh I (字符串,水)

    题意:设有两个串A和B,现将B反转,再用插入的形式合成一个串.如:A:abc   B:efg:反转B先,变gfe:作插入,agbfce.现在给出一个串,要求还原出A和B. 思路:扫一遍O(n),串A在 ...