去除重複的列:mysql
mysql> select distinct deptno from emp;sql
條件查詢:ide
mysql> select * from emp where deptno=1 ;排序
mysql> select * from emp where deptno=1 and sal < 3000;it
排序和限制:io
mysql> select * from emp order by sal;class
mysql> select * from emp order by deptno;select
mysql> select * from emp order by deptno, sal desc;rollup
mysql> select * from emp order by sal desc limit 3;nio
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;