SQL資料庫select基本使用

2021-08-10 04:40:25 字數 3538 閱讀 3687

select基本語句

基本語法:

select 列名 from  表名

【例】從學生表(student)中查詢所有學生的學號        單列

select 學號 from  student

【例】查詢課程表課程號、課程名、學分         多列用逗號「,」分隔符

select 課程號,課程名, 學分 from course

【例】查詢學生表(student)中的所有資訊       所有列用』 * 』 代替

select  *  from  student

當在select語句指定列的位置上使用*號時,表示選擇表的所有列。

條件查詢 

【例】查詢student表中姓名為李明的相關資訊         用where指明條件

[sql]

view plain

copy

select

*   

from

student  

where

姓名=』李明』  

【例】查詢student表中出生日期在1989-1-3以後的學生

[sql]

view plain

copy

select

姓名,出生日期  

from

student  

where

出生日期》』1989-1-3』  

注:字元型或日期型的資料要用單引號引起來

and ,or

【例】查詢student表中學號為1101且姓名為李明的相關資訊 

[sql]

view plain

copy

select

*   

from

student  

where

姓名=』李明』 

and學號=『1101』  

【例】查詢student表中姓名為李明或者學號為1101的相關資訊

[sql]

view plain

copy

select

*   

from

student  

where

姓名=』李明』 

or學號=『1101』  

between…and 在…範圍之內

【例】查詢總學分為60和70之間的學生所有資訊 

[sql]

view plain

copy

select

* from

student 

where

總學分 

between

60 and

70 --(大於60小於70)

【例】查詢出生日期不在1980-8-26到1979-1-29的學生姓名

[sql]

view plain

copy

select

姓名,出生日期   

from

student  

where

出生日期 

notbetween

'1980-8-26 '

and『1979-1- 29 『  

* 注意時間的用法

in ==or

in用於查詢屬性值屬於指定集合的記錄,與in相對的謂詞是not in,in 關鍵字可以簡化查詢條件的書寫

【例】查詢分數為70和80的所有學生資訊

[sql]

view plain

copy

use grade  

select

* from

xs  

where

總學分 

in(70,80)   

is關鍵字

在基本表中,如果那一列中沒有輸入資料,則它的值就為空,空值用乙個特殊的資料null來表示,如果要判斷某一列是否為空,不能用「=null」或「 <>null」來比較,只能用is null或is not null來運算

例:查詢郵箱為空的學生記錄

[delphi]

view plain

copy

select * from student   

where email is

null  

distinct關鍵字

distinct:從返回的結果資料集合中刪除重複的行

【例】查詢grade資料庫中student表中的總學分,但是不能有重複的

[sql]

view plain

copy

use grade  

select

distinct

總學分  

from

student  

order

by總學分  

go  

使用like子句進行模糊查詢

like子句與萬用字元配合使用。sqlserver提供4種萬用字元

1.%:表示任意字元

2. _:表示單個任意字元

3.[ ]:表示方括號裡列出的任意乙個字元.

4.[^]:表示任意乙個沒有在方括號裡列出的字元.

基本語法:

select 欄位名 from 目標表 where 欄位名 like 條件

排序查詢

order by排序:asc公升;desc降

【例】查詢學生的總學分以公升序排列,出生日期以降序排列的學生姓名和學號

[sql]

view plain

copy

use grade  

select

姓名,出生日期,總學分, 學號  

from

student  

order

by總學分 

asc,出生日期 

desc

*以第一列為主序,再在第一列相等的基礎上再對第二列排序;asc 預設,可省略

top關鍵字

top:關鍵字用於指定只返回前面一定數量的資料.

top n :表示返回最前面的n行.

【例】查詢在grade庫中student表中總學分最高的前5項的學生姓名

[sql]

view plain

copy

use grade  

select

top5 總學分, 姓名     

from

student  

order

by總學分 

ascgo  

top. ..with ties      指定返回並列前n條記錄

[sql]

view plain

copy

use northwind  

select

top5 

with

ties orderid, productid, quantity  

from

[order

details]  

order

byquantity 

desc

go  

* with ties必須與order by連用

SQL資料庫基本操作

1 建立表 create create table dba test 建立表,表名為dba test col1 number,col2 varchar2 10 第一列預設值是0 第二列不准許有空值 第一列預設值是0 第二列不准許有空值 2 檢索操作 select select 1 from 2 其中...

資料庫基本sql語句

建立資料庫 1 如果不存在就新建資料庫,使用utf 8編碼格式,預設使用utf8排序。2create database ifnot exists test default character setutf8 collate utf8 general ci3 刪除資料庫 4drop database ...

android資料庫sqlite的基本使用

android資料庫sqlite的基本使用 首先使用sqliteopenhelper類的基本方法,應用sqlitedatabase的類建立出資料庫物件,在context.openorcreatedatabase 方法例項化出資料庫,這樣對資料庫可進行操作了,有對資料庫建立表,然後對錶進行插入資料,更...