SQL 簡單的 CRUD 操作

2021-08-28 02:53:39 字數 3352 閱讀 3016

sql 是用於訪問和處理資料庫的標準的計算機語言,全稱是 structured query language。

注:sql 對大小寫不敏感, select 和 select 是相同的。

insert into table_name values (value1,value2,value3,...);

或者insert into table_name (column1,column2,column3,...)

values (value1,value2,value3,...);

例子:

insert

into article(type, title, author, date, source, content, arttag)

values ('1111','人生','一公尺陽光','2018-09-06 09:01','人生知網','有多沉默,就有多心痛。','3213');

update 語句用於更新表中的記錄。

update table_name

set column1=value1,column2=value2,...

where some_column=some_value;

例子:

update article 

set author='飄雪',title='人生感悟'

where articleid = '12334';

注:請注意 sql update 語句中的 where 子句!

where 子句規定哪條記錄或者哪些記錄需要更新。如果您省略了 where 子句,所有的記錄都將被更新!

delete 語句用於刪除表中的記錄。

delete

from table_name

where some_column=some_value;

例子:

delete

from article

where type = '1111';

注:請注意 sql delete 語句中的 where 子句!

where 子句規定哪條記錄或者哪些記錄需要刪除。如果您省略了 where 子句,所有的記錄都將被刪除!

從資料庫中提取資料:

select column_name1,column_name2 from table_name;

select * from table_name;

例子:

select title,author from article;
乙個列表中可能會包含多個重複的值,如果你希望列出不同(distinct)的值,你可以使用distinct關鍵字返回唯一的值。

select

distinct column_name1,column_name2 from table_name;

條件子句,篩選滿足條件的記錄。

select column_name1,column_name2

from table_name

where column_name operator value;

例子:

select title,author,date

from article

where

date >= '2018-08-01';

where 子句中的一些運算子:

1、比較運算子

=、>、<、>=、<=、!=、<>(不等於)

2、邏輯運算子

and、or、not

select title,author,date

from article

where (author = '蘭渡'

or author = '陽光下奔跑的雪') and

date >= '2018-05-01'

3、特殊

is null(空值判斷)、between and(在之間)、in、like

例子:

select title,author 

from article

where author is

null

select title,author,date

from article

where

date between '2018-05-01'

and'2018-08-01'

select title,author,date

from article

where author in ('蘭渡','秋色','陽光下奔跑的雪')

select title,author,date

from article

where title like

'人生%'

like注:

m 為要查詢內容中的模糊資訊,% 表示多個字值,_ 下劃線表示乙個字元;

m% : 為能配符,正規表示式,表示的意思為模糊查詢資訊為 m 開頭的。

%m% : 表示查詢包含m的所有內容。

%m_ : 表示查詢以m在倒數第二位的所有內容。

order by 關鍵字用於對結果集按照乙個列或者多個列進行排序。預設按照公升序(asc),使用(desc)關鍵字對記錄進行降序排序。

select column_name1,column_name2

from table_name

order

by column_name,column_name asc|desc;

例子:

select title,author,date

from article

order

bydate

desc, title; // 先按照date降序,再按照title公升序

limit 可以限制返回的數量,offset 可以設定偏移的點。在做資料分頁時,你可以使用到這兩個關鍵字,例如,第3頁起的10條資料。

select title,author,date

from article

order

bydate

desc

limit 10 offset 20 // 等同於 limit 20,10

以上內容摘抄自菜鳥教程sql篇。

簡單操作crud

資料的操作 dml 建立資料 插入資料 insert into tbl name 字段列表 values 值列表 insert into exam student name,stu no values zhangsan php001 如果需要在插入時,為所有的字段設定值,那麼可以省略字段列表。要求是...

簡單操作crud

資料的操作 dml 建立資料 插入資料 insert into tbl name 字段列表 values 值列表 insert into exam student name,stu no values zhangsan php001 如果需要在插入時,為所有的字段設定值,那麼可以省略字段列表。要求是...

MyBatis Plus的CRUD 簡單操作

crud 是指在做計算處理時的增加 create 讀取查詢 retrieve 更新 update 和刪除 delete 幾個單詞的首字母簡寫。增加操作 resource test public void insert 執行完成後的 查詢操作 test public void selectbyname...