資料庫中表的複雜查詢 分頁

2021-09-08 04:38:56 字數 1980 閱讀 4574

一、資料庫中表的複雜查詢

1)連線查詢

1.0連線的基本的語法格式:

from table1 join_type table2 [on (join_condition)][where (query_condition)]

table1:左表

table2:右表

join_type:連線的型別。交叉、內連線、左外連線、右外連線

on:設定連線條件

where:對連線查詢的結果進步一的篩選

1.1交叉連線

select * from customer cross join orders;

或者select * from customer,orders;

select c.name,o.order_number from customer c,orders o;

1.2內連線:

隱式內連線:(不使用onkeyword,使用where)

select * from customer c,orders o where c.id=o.customer_id;

顯式內連線:(使用onkeyword)

select * from customer c inner join orders o on c.id=o.customer_id;

1.3外連線:

左外連線:(返回符合連線條件的全部記錄。同一時候還返回左表中其餘的全部記錄)

select * from customer c left outer join orders o on c.id=o.customer_id;

右外連線:(返回符合連線條件的全部記錄。同一時候還返回右表中其餘的全部記錄)

select * from customer c right outer join orders o on c.id=o.customer_id;

2)子查詢(巢狀查詢)

子查詢: select * from orders where customer_id=(select id from customer where name='張三');

3)聯合查詢

select * from orders where price>200 union select * from orders where customer_id=1;

取兩條語句的並集。並去除反覆的記錄。

二、資料庫中的分頁:

1)mysql中的分頁:(limit m,n 注:m表示開始的下標,n表示要查詢的條數)

select * from table where … limit 1; #返回第0行

select * from table where … limit 10; #返回0到9行

select * from table where … limit 0,10; #返回0到9行:從0開始,查10條

select * from table where … limit 10,15 #返回10到24行:從10開始,查15條

2)oracle中的分頁:

1)oracle中的rownum和rowid都是偽列,可是兩者在根本上是不同的:rownum是依據sql查詢出的結果給每行分配乙個邏輯編號。所以你的sql不同也就會導致終於rownum不同。

注意:rownum是從1開始記錄的rowid是物理結構上的,在每條記錄insert到資料庫中時,都會有乙個唯一的物理記錄。rowid具有唯一性。 2)通常在sql分頁時或是查詢某一範圍內的記錄時使用rownum;在處理一張有反覆記錄的表時經經常使用到rowid。 樣例一:查詢小於3的記錄(注意:假設直接用rownum,則查詢的範圍必須包括1) sql:select * from emp a where rownum < 3; 樣例二:查詢2到10範圍內的記錄(這裡包括2和10的記錄) 策略:把rownum查出來後放在乙個虛表中作為這個虛表的字段再依據條件查詢 sql:select * from (select rownum rn, a.* from emp a) t where t.rn between 2 and 10;

查詢資料庫中表的資訊

select 表名 case when a.colorder 1 then d.name else end,表說明 case when a.colorder 1 then isnull f.value,else end,字段序號 a.colorder,欄位名 a.name,標識 case when ...

查詢資料庫中表資訊等

有時候需要匯出某使用者下的所有table view sequence trigger等資訊,下面的sql可以將這些資訊select出來 select from user tables select from user views select from user sequences select f...

查詢MySQL資料庫中表結構

什麼是表結構?表結構就是定義資料表檔名,確定資料表包含哪些字段,各字段的欄位名 字段型別 及寬度,並將這些資料輸入到計算機當中。查詢方法 以表 employees 為例子 1.describe desc 表名 desc 是 describe的縮寫 describe用於檢視特定表的詳細設計資訊 des...