mysql sql 语句使用:

插入: 


insert into b(a, b, c) select d,e,f from b;

 create table test as select * from dept; --从已知表复制数据和结构  

 create table test as select * from dept where 1=2; --从已知表复制结构但不包括数据  


查询: 


包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表

(select a from tableA ) except (select a from tableB) except (select a from tableC)



 SELECT * FROM tb_stu  WHERE sname like '%PHP%'

% 会吻合任何字数,包括 0。

_ 会吻合单一字数, _at 会吻合 cat, mat, bat...



分组查询总结:

1.WHERE子句从数据源中去掉不符合其搜索条件的数据

2.GROUP BY子句搜集数据行到各个组中

3.统计函数为各个组计算统计值

4.HAVING子句去掉不符合其组搜索条件的各组数据行

5使用GROUP BY时,select后面出现的内容要么为聚合函数,要么为group by后面出现的内容





二、递归查询 
1.语法 

Sql代码  

1.   Select * from …. Where [结果过滤条件语句]  

2.   Start with  [起始条件过滤语句]  

3.   Connect by prior [中间记录过滤条件语句]   


2.例子 

Sql代码  

1.   Select * from company t Where t.flag=1  

2.   Start with  t.company_id=50500000  

3.   Connect by prior t.company_id=t.parent_id  


说明: 

Sql代码  

1.   select [level],column,expr from table [where condition]  

2.   [start with] //[起点]  

3.   [connect by prior + 主键=外键 或 外键=主键]  


a.自顶向下: 左边放主键,右边放外键。 
b.自底向上: 右边放主键,左边放外键。 
c.level(伪列)层次的级别,不固定值。 



函数:


一、字符串函数【比较常用,需要掌握】

1、 concat(s1,s2,...,sn) #把传入的参数连接成一个字符串

select concat('abc','def');

select concat(name,' age is ',age) from users;



2、insert(str,m,n,inser_str) #将str的从m位置开始的n个字符替换为inser_str

select insert('abcdef',2,3,'123456');

select insert(name,3,2,'HAHA') from users;

select insert(name,2,2,'00') from users;



3、lower(str)/upper(str) #将字符串str转换成小写/大写

selectlower('HELLO'),upper('hello');

selectlower('HELLO') as 'HELLO',upper('hello')as 'HELLO';

select* from users where upper(name) = 'AAA';



4、left(str,n)/right(str,n) #分别返回str最左边/最右边的n个字符,如果n<=> NULL 则任何东西不返回

selectleft('123',3),right('123456',3),left('123',NULL);



5、lpad(str,n,pad)/rpad(str,n,pad) #用字符串pad对str的最左边/最右边进行填充,知道满足str含有n个字符为止

selectname,lpad(name,10,'#'),rpad(name,10,'@') from users;



6、trim(str)/ltrim(str)/rtrim(str) #去除字符串str左右空格/左空格/右空格

selectconcat('#',trim(" abc "),'#'),concat('#',ltrim(' abc '),'#'),concat('#',rtrim(' abc '),'#');



7、replace(str,sear_str,sub_str) #将字符串str中所有出现的sear_str字符串替换为sub_str

select replace('abcdefgabcd','cd','XXX') ;



8、strcmp(str1,str2) #以ASCII码比较字符串str1,str2,返回-1(str1< str2)/0(str1= str2)/1(str1 > str2)

selectstrcmp('aa','bb'),strcmp('aa','aa'),strcmp('bb','aa');



9、substring(str,n,m) #返回字符串str中从n起,m个字符长度的字符串

selectsubstring('abcdef',2,3);

selectname,substring(name,1,2) as subname from users;



二、数值函数

1、abs(x) #返回x的绝对值

selectabs(10),abs(-10);

selectabs(age) from users;



2、ceil(x) #返回大于x的最小整数

3、floor(x) #返回小于x的最大整数

selectceil(2.1),ceil(2.5),ceil(2.9),floor(2.1),floor(2.5),floor(2.9);



4、mod(x,y) #返回x/y的模,与x%y作用相同

selectmod(null,11);



5、rand() #返回0~1之间的随机数

selectrand();

selectceil(rand() * 100); #取0~100之间的整数随机数

selectfloor(rand() * 100);



6、round(n,m) #返回n四舍五入之后含有m位小数的值,m值默认为0

selectround(1.23);

selectround(1.456,2);



7、truncate(n,m) #返回数字n被截断为m位小数的数值

selecttruncate(1.234,2);

selecttruncate(1.235,2),round(1.235,2);



三:日期函数:


当前日期,当前时间,当前日期+时间,当前时间戳,把时间戳转化为日期+时间。

select CURDATE(),CURTIME(),NOW() ,UNIX_TIMESTAMP(NOW()), FROM_UNIXTIME(unix_timestamp(NOW()));



select month(create_time) from t_union_showtime;

select year(create_time) from t_union_showtime;

select day(create_time) from t_union_showtime;

…  week hour minute second 


select monthname(create_time) from t_union_showtime;


select  date_format('2015-1-8',"%Y-%m-%d %H:%i%s") from t_union_showtime;

create_time 是datetime 类型的也可以:

select  date_format(create_time,"%Y-%m-%d %H:%i%s") from t_union_showtime;



日期格式:

date_format(now(),"%Y-%M-%D%H:%I%S") #将当期时间格式化

selectdate_format(now(),"%Y-%m-%d %H:%i%s");

selectdate_format(now(),"%y%m%d %H:%i%s");



四控制流向的函数:


select IF(month(create_time)=1,1,0) from t_union_showtime;

select CASE month(create_time) WHEN 11 THEN 1 ELSE 2 END from t_union_showtime;

select  IFNULL(create_time,0) from t_union_showtime;




五、其他函数

1、database() #当前数据库

2、version() #当前数据库版本

3、user() #当前登录用户

selectdatabase();

4、inet_aton(ip) #ip地址的网络字节顺序

selectinet_aton('192.168.139.1');

5、inet_ntoa #返回数字所代表的ip

selectinet_ntoa(3232271105);

6、password(str) #返回加密的str字符串

selectpassword("123456"); #返回一个41位长的加密字符串,只是用于给MySQL系统用户进行加密

7、md5() #在应用程序中进行数据加密,比如在C++程序中

selectmd5(“123456”);





你可能感兴趣的:(mysql sql 语句使用:)