常見的sql查詢語句有:1、檢視表格結構【SQL>DESC emp;】;2、查詢所有欄位【SQL>SELECT * FROM emp】;3、查詢指定欄位【SQL>SELECT empmo ,】;4、查詢指定行【SQL>SELECT * FROM】。
常見的sql查詢語句有:
一、簡單查詢語句
1. 檢視表格結構
SQL>DESC emp;
2. 查詢所有欄位
SQL>SELECT * FROM emp;
3. 查詢指定列
SQL>SELECT empmo, ename, mgr FROM emp;
SQL>SELECT DISTINCT mgr FROM emp; 只顯示結果不同的項目
4. 查詢指定行##
SQL>SELECT * FROM emp WHERE job='CLERK';
5 . 使用算術表達式
SQL>SELECT ename, sal*13+nvl(comm,0) FROM emp;
SQL>SELECT * FROM emp WHERE hiredate>'01-1月-82';
#6. 使用like操作符(%,_)
%表示一個或多個字符,_表示一個字符,[charlist]表示字符列中的任何單一字符,[^charlist]或[!charlist]不在字符列中的任何單一字元。SQL>SELECT * FROM emp WHERE ename like 'S__T%';
7. 在where條件中使用In
SQL>SELECT * FROM emp WHERE job IN ('CLERK','ANALYST');
8. 查詢欄位內容為空/非空的語句
SQL>SELECT * FROM emp WHERE mgr IS/IS NOT NULL;
9. 使用邏輯操作符號
SQL>SELECT * FROM emp WHERE (sal>500 or job='MANAGE') and ename like 'J%';
10. 將查詢結果按字段的值進行排序
SQL>SELECT * FROM emp ORDER BY deptno, sal DESC;
11. 使用case ... when ... then ... end處理查詢結果
SQL>SELECT CASE a WHEN "original_a" THEN "新命名Aa" WHEN "original_b" THEN "新命名Bb" END AS XXX;選擇表中的a字段並命名為XXX,當a的內容為original_a時,內容展示為「新命名Aa」。12. 格式化日期資料
SQL>SELECT DATE_FORMAT(start_time,"%Y-%m-%d") as "时间";
#二、複雜查詢
1. 資料分組(max ,min,avg,sum,count)
SQL>SELECT MAX(sal),MIN(age),AVG(sal),SUM(sal) from emp; SQL>SELECT * FROM emp where sal=(SELECT MAX(sal) from emp)); SQL>SELEC COUNT(*) FROM emp;
2. group by(用於對查詢結果的分組統計) 和having子句(用於限制分組顯示結果)
SQL>SELECT deptno,MAX(sal),AVG(sal) FROM emp GROUP BY deptno; SQL>SELECT deptno, job, AVG(sal),MIN(sal) FROM emp group by deptno,job having AVG(sal)<2000;
3. 多表查詢
SQL>SELECT e.name,e.sal,d.dname FROM emp e, dept d WHERE e.deptno=d.deptno order by d.deptno; SQL>SELECT e.ename,e.sal,s.grade FROM emp e,salgrade s WHER e.sal BETWEEN s.losal AND s.hisal; Select a.*, b.x, c.y from a left outer join (Select * from tablex where condition1)b on a.id=b.id left outer join (Select * from tabley where condition2)c on a.id=c.id where condition3;
4. 自連接(指同一張表的連接查詢)
SQL>SELECT er.ename, ee.ename mgr_name from emp er, emp ee where er.mgr=ee.empno;
5. 子查詢(嵌入到其他sql語句中的select語句,也叫巢狀查詢)
5.1 單行子查詢SQL>SELECT ename FROM emp WHERE deptno=(SELECT deptno FROM emp where ename='SMITH');
SQL>SELECT ename,job,sal,deptno from emp WHERE job IN (SELECT DISTINCT job FROM emp WHERE deptno=10);
SQL>SELECT ename,sal,deptno FROM emp WHERE sal> ALL (SELECT sal FROM emp WHERE deptno=30);或SQL>SELECT ename,sal,deptno FROM emp WHERE sal> (SELECT MAX(sal) FROM emp WHERE deptno=30);
SQL>SELECT ename,sal,deptno FROM emp WHERE sal> ANY (SELECT sal FROM emp WHERE deptno=30);或SQL>SELECT ename,sal,deptno FROM emp WHERE sal> (SELECT MIN(sal) FROM emp WHERE deptno=30);
SQL>SELECT * FROM emp WHERE (job, deptno)=(SELECT job, deptno FROM emp WHERE ename='SMITH');
SQL>SELECT emp.deptno,emp.ename,emp.sal,t_avgsal.avgsal FROM emp,(SELECT emp.deptno,avg(emp.sal) avgsal FROM emp GROUP BY emp.deptno) t_avgsal where emp.deptno=t_avgsal.deptno AND emp.sal>t_avgsal.avgsal ORDER BY emp.deptno;
SQL>SELECT a2.* FROM (SELECT a1.*, ROWNUM rn FROM (SELECT * FROM emp ORDER BY sal) a1 WHERE ROWNUM<=10) a2 WHERE rn>=6;
SQL>CREATE TABLE mytable (id,name,sal,job,deptno) AS SELECT empno,ename,sal,job,deptno FROM emp;
SQL>SELECT ename, sal, job FROM emp WHERE sal>2500 UNION(INTERSECT/UNION ALL/MINUS) SELECT ename, sal, job FROM emp WHERE job='MANAGER';
合并查询的执行效率远高于and,or等逻辑查询。
5.10 使用子查询插入数据
SQL>CREATE TABLE myEmp(empID number(4), name varchar2(20), sal number(6), job varchar2(10), dept number(2));
先建一张空表;
SQL>INSERT INTO myEmp(empID, name, sal, job, dept) SELECT empno, ename, sal, job, deptno FROM emp WHERE deptno=10;
再将emp表中部门号为10的数据插入到新表myEmp中,实现数据的批量查询。
5.11 使用了查询更新表中的数据
SQL>UPDATE emp SET(job, sal, comm)=(SELECT job, sal, comm FROM emp where ename='SMITH') WHERE ename='SCOTT';
相关免费学习推荐:SQL视频教程
以上是常見的sql查詢語句有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!