class07 查詢資料

2022-06-27 10:27:10 字數 1622 閱讀 1729

查詢資料:關鍵字select

select字段列表from表名;

【查詢出來的結果集無互異性

例子:select stu_id from student;

查詢多個列:select stu_id,stu_name,gender from student;

select * from 表名;

*:也可以是手動寫出所有欄位名,顯示字段順序與輸入的順序一致

關鍵字:order by ...[asc:公升序排序 / desc:降序排序]

select欄位列表from表名order by 欄位1【asc/desc】,欄位2【asc/desc】……;

【注意:】order by 欄位名,預設是asc方式

欄位名也可以用select 後面結果的順序,直接用順序數字代替:

例子:

selectstu_id,stu_name,age from student order by stu_id asc,age asc;

等價於 ===:

selectstu_id,stu_name,age from student order by   1 asc, 3 asc;

【一般不太建議】

返回前幾行:關鍵字top、limit

兩種寫法

一般在兩種情況下會使用:

例子:

select  stu_id from student order by stu_id asc,age asc limit 3;

​selecttop 3 stu_id from student order by stu_id asc,age asc;

[注意:]

mysql不支援top寫法,但是支援limit

用法

返回中間幾行:關鍵字: limit m offset n【從第n行開始,返回m行記錄】n:從0開始

兩種寫法:

select 字段列表 from 表名 limit m offset n;

select 字段列表 from 表名 limit n,m;

注意:select 字段列表 from 表名 limit 0,10; === select 字段列表 from 表名 limit 10;

返回後幾行:關鍵字:top、limit、order by

sql語句中,沒有返回後幾行的專用寫法,一般轉換為按返回前幾行的相反方式排序後,再返回前幾行。使用這種方式變相的返回後幾行。

例子:

select * from(select * from teacher order by teacher_id desc limit 5)  **as button_5** order by teacher_id;

【注意:】這裡的兩個select已經屬於聯合查詢了,因此要給這個子查詢取乙個別名,這裡取名:button_5

07 MongoDB 資料查詢

方法find 查詢 db.集合名稱.find 方法findone 查詢,只返回第乙個 db.集合名稱.findone 方法pretty 將結果格式化 db.集合名稱.find pretty 等於,預設是等於判斷,沒有運算子 小於 lt 小於或等於 lte 大於 gt 大於或等於 gte 不等於 ne...

SSAS系列 07 多維資料(查詢Cube)

原文 ssas系列 多維資料 查詢cube 1 什麼是mdx?mdx叫做 多維表示式 是一種查詢語言,是一種和sql類似的查詢語言,它基於 xml for analysis xmla 規範,並帶有特定於 sql server analysis services 的擴充套件。2 mdx與sql有什麼區...

python演算法與資料結構 07查詢

1.遞迴版本 def binarysearch1 alist,item 二分查詢前提 有序陣列 二分查詢遞迴版本 時間複雜度 o logn n len alist if0 n return false mid n 2if alist mid item return true elif alist m...