1 外键 foreign key (字段) references 表名(字段);
2 读取文件中的命令: 可在mysql中键入?查询相关命令
\. c:\\mysql_script\\mydata.sql
3 create table table_name
(
id int primary key auto_increment,
title varchar(255)
);
drop table table_name1,table_name2;
drop database database_name;
4 select * from table_name [order by id desc limit 2,2];
select field1,field2.... from table_name[codition];
select distinct(与其他不同的) field(*) from table_name;
select * from table_name[condition];
select * from table_name order by field_name;//按照字段值由小到大排序
select * from table_name order by field_name desc;//按照字段值由大到小排序
5 insert into table_name values(2,'如果你觉得我不够好,那是因为你把我想的太完美');
insert into table_name(title) values('爱你不需要理由,但我还是有一万个理由爱你');
insert into table_name(field1,field2) values(value1,value2),(value1,value2);
6 desc table_name;
7 alter table table1 rename table2
1 select now();显示但前日期 2008-09-03 09:59:11
select date_format(now(),'%Y-%m-%d %h:%i:%s'); 2008-09-03 09:59:11
select date_format(now(),'%Y年%m月%日 %h:%i:%s'); 2008年09月03日 09:59:11
select date_format(now(),'%Y-%m-%d'); 2008-09-03
2 alter table table_name modify field_name someprofilechange;//更改表中字段的属性
alter table table_name modify field_name field_type first;//将字段移植第一行
alter table table_name add field_name field_type;//添加一个字段(默认末尾 first,after,last)
3 update table_name set field_value where field_key;
update table1,table2,...set table1.field1=?,table2.field=? [where cpndition];
4 delete from table_name [condition];
delete a,b from table1 a, table2 b [condition];
5 聚合:
select count(1)(count(*),count(field),count(distinct field)) from emp;//统计emp(某个 记录)中的记录数
select field,count(1) from emp group by field;//统计field字段出现的次数
select field,count(1) from emp group by field1,field2 with rollup;//统计field1与 field2组合字段出 现的次 数和总的记录数
总结: select [field1,field2...] fun_name(sum,count(*),max,min) from table_name [where condition] [group by field1,field2...] [with rollup] [having where_condition];
select sum(sal),max(sal),min(sal) from emp;
6 表连接
select table1_field_name,table2_field_name from table1,table2 [where condition];列出合法的记录
select table1_field_name,table2_field_name from table1 left join table2 on [condition]:select ename,deptname from emp left join on emp.deptno=dept.deptno;左 连接,包含所有左边表的记录,甚至所有右边表中没有的记录
相似有右连接:
select table1_field_name,table2_field_name from table1 right join tale2 on [condition];
7 子查询
select * from table1 where table1_field in(select table2_field from table2);
8 日期和时间函数
select curdate(); 2008-09-07
select curtime();
select now();
select unix_timestamp(now());返回日期date的unix时间?
select from_unixtime(unixtime);
select week(now()),year(now()),hour(curtime()),minute(curtime());
select monthname(now());
select date_format(now(),'%m,%d,%h');
select now() current,date_add(now(),interval 31 day) after31days,date_add(now (),interval '1_2' year_month) after_oneyear_twomonth;
select datediff('2008-08-08',now());
9 流程函数(salary为表名)
select if(salary>2000,'high','low') from salary; if(value,t,f);
select ifnull(salary,0) from salary; ifnull(value1,value2);
select case when salary<=2000 then 'low' else 'high' end from salary;
case when [value] then [result]...else [default] end函数
select case salary when 1000 then 'low' when 2000 then 'mid' else 'high' end from salary;
10 其他函数
select database();
select version();
select user();
select inet_aton(ip);返回ip地址的网络字节序表示
select inet_ntoa(num);
select * from table where inet_aton(ip) >= inet_aton('192.168.1.1) and inet_aton (ip) <= inet_aton('192.168.1.255');//返回两ip地址之间的ip
select password(str);//返回str的加密版本,一个41位长的字符串
select md5(str); //返回str的md5值,常用来给数据进行加密
11 select formate(1233.42,2);返回1,233.42,2表示有两位小数,0表示无小数(都要四舍五入)
12 自连接
select e1.ename, e2.ename from emp e1, emp e2 [where e1.ename=e2.empno]
13 sql1999
select e1.ename ,e2.ename from emp e1 left join emp e2 on(e1.mgr=e2.empno);
select ename,deptname from emp join dept using(deptno);
select ename,deptname from emp cross join dept;交叉连接,笛卡尔乘积
select e1.ename ,e2.ename from emp e1 join emp e2 on(e1.mgr=e2.empno);
select e1.ename ,e2.ename from emp e1 right outer join emp e2 on(e1.mgr=e2.empno);
select e1.ename ,e2.ename from emp e1 full join emp e2 on(e1.mgr=e2.empno);
三个表连接
select ename,deptname,grade from emp e join dept d on(e.deptno = d.deptno) join salgrade s on(e.sal between s.losal and s.hisal) where ename not like '_A%';
本文出自 “小杨” 博客,请务必保留此出处http://yqsshr.blog.51cto.com/469059/100522