去除重复的列:
mysql> select distinct deptno from emp;
条件查询:
mysql> select * from emp where deptno=1 ;
mysql> select * from emp where deptno=1 and sal < 3000;
排序和限制:
mysql> select * from emp order by sal;
mysql> select * from emp order by deptno;
mysql> select * from emp order by deptno, sal desc;
mysql> select * from emp order by sal desc limit 3;
mysql> select * from emp order by sal desc limit 1,3;
聚合:
mysql> select count(1) from emp;
mysql> select deptno,count(1) from emp group by deptno;
mysql> select deptno,count(1) from emp group by deptno with rollup;
mysql> select deptno,count(1) from emp group by deptno having count(1)>1;
mysql> select sum(sal),max(sal),min(sal) from emp;
表连接:
mysql> select ename,deptname from emp,dept where emp.deptno=dept.deptno;
mysql> select ename,deptname from emp left join dept on emp.deptno=dept.deptno;
mysql> select ename,deptname from dept right join emp on dept.deptno=emp.deptno;
子查询:
mysql> select * from emp where deptno in(select deptno from dept);
mysql> select * from emp where deptno = (select deptno from dept);
mysql> select * from emp where deptno = (select deptno from dept limit 1 );
mysql> select emp.* from emp,dept where emp.deptno = dept.deptno ;
记录联合:
mysql> select deptno from emp union all select deptno from dept;
mysql> select deptno from emp union select deptno from dept;