使用order by 子句對查詢結果進行排序
排序方式包括公升序(asc,可省略)和降序(desc)兩種:
按多字段排序:select empno, ename, sal from emp order by sal
select empno, ename, sal from emp order by sal desc
select deptno, empno, ename, sal from emp order by deptno, sal
使用字段別名排序:
select empno, ename, sal*12 annsal from emp order by annsal
用法舉例:
查詢條件中可以使用比較運算子:select * from emp where deptno=10
select * from emp where ename='smith'
select * from emp where hhiredate='02-4月-81'
--注意:
--字串和日期要用單引號括起來
--字串大小寫敏感
--日期格式敏感,預設的日期格式是'dd-mon-rr'
使用like運算子執行模糊查詢(通配查詢)
%表示零或多個字元 _表示乙個字元
對於特殊符號可使用escape識別符號查詢
用法舉例:
使用is null運算子進行空值判斷select * from emp where ename like 's%'
select * from emp where ename like '_a%'
select * from emp wher ename like '%\%' escape ' \'
用法舉例:
查詢條件可以使用邏輯運算子select * from emp where comm is null
select * from emp where comm is not null
sql優化問題:select * from emp where deptno = 10 and sal > 1000
select * from emp where deptno = 10 or job = 'clerk'
and:把檢索結果較少的條件放到後面
or:把檢索條件較多的放到後面
共計四種運算子:算術》連線》比較》邏輯
可以使用小括號強行改變運算順序
hql語句 單錶查詢
在hibernate中,有下列比較常用的查詢語句 1 hql hibernate query language 2 criteria queries 條件查詢 3 native queries 原生態sql語句查詢 最常用的就是hql,那麼 使用hql進行查詢,又分為 單錶查詢,一對多和多對多查詢,...
SQL語句 單錶查詢
select from tableselect col1,col2 from tableselect from table where condition 篩選數字屬性列 篩選字串屬性列 通過limit選取部分結果 選取前n行的記錄 select from table where condition...
SELECT簡單查詢
簡單查詢包括查詢資料表中的所有資料 部分字段資訊等,基本語法為 1 查詢出表中的所有資料 select from table name 2 查詢出表中特定欄位的資料 select column name1,column name2 from table name 在資料表中,資料可能存在重複情況,比...