mysql常用語句(基礎)

2021-10-01 21:36:50 字數 2363 閱讀 7159

#1.select語句 (語法:select 列名稱 from 表名稱)

#* 查詢全部

select

*from

user

;#select distinct 語句用法 (用於返回唯一不同的值。)

select

distinct user_age from

`user`;

#2.where 子句 根據條件查詢 (語法:select 列名稱 from 表名稱 where 列 運算子 值)

#between 運算子的用法 (返回在某個範圍內的結果)

select

*from

user

where user_age between

21and22;

#like 運算子的用法 用於在 where 子句中搜尋列中的指定模式

#(模糊查詢,返回過濾結果,"%" 可用於定義萬用字元(模式中缺少的字母))

select

*from

user

where user_name like

'tes%6'

;#and 運算子的用法 (如果第乙個條件和第二個條件都成立,則 and 運算子顯示一條記錄。)

select

*from

user

where user_name =

'test'

or user_age =18;

#or 運算子的用法 (如果第乙個條件和第二個條件中只要有乙個成立,則 or 運算子顯示一條記錄。)

select

*from

user

where user_name =

'test'

or user_age =18;

#結合 and 和 or 運算子 (使用圓括號來組成複雜的表示式)

select

*from

user

where

(user_name=

'test3'

or user_name =

'test6'

)and

(user_age =

18or user_age=

123)

;#3.order by 子句 用於根據指定的列對結果集進行排序。

#order by 用法 (ase:公升序,desc降序,注第乙個字段相同時,第二個欄位的排序才會生效)

select

*from

user

order

by user_name asc

, user_age desc

;#4.insert into 語句 用於向**中插入新的行

#(語法:insert into 表名稱 values (值1, 值2,....)或 insert into table_name (列1, 列2,...) values (值1, 值2,....))

#insert into 用法 (id 為自增長時,可以用null代替,或用下面第二種方式)

insert

into

user

values

(null

,'test1',20

,"123456");

#insert into 第二種 方式 在指定的列中插入資料

insert

into

user

(user_name,user_age)

values

('test2',22

);#5.update 語句 用於修改表中的資料。 (語法:update 表名稱 set 列名稱 = 新值 where 列名稱 = 某值)

#update語句用法 更新某一行中的乙個列

update

user

set user_name =

'altered'

where id =32;

#update語句用法 更新某一行中的若干列

update

user

set user_name =

'altered'

, user_age =

20where id =32;

#6.delete 語句 用於刪除表中的行。(語法:delete from 表名稱 where 列名稱 = 值)

#delete語句用法 刪除某一行

delete

from

user

where id =32;

#delete語句用法 刪除所有行 (可以在不刪除表的情況下刪除所有的行。這意味著表的結構、屬性和索引都是完整的)

delete

from

user

;

mysql常用語句 MySQL常用語句

create table student id int primary key auto increment comment 學號 name varchar 200 comment 姓名 age int comment 年齡 comment 學生資訊 修改表注釋 alter table studen...

php mysql 常用語句 mysql常用語句

一 修改mysql使用者密碼 mysql h localhost u root p 命令列登入 update user set password password 123456 where user root 二 資料庫操作 show databases 顯示資料庫 create database ...

MySQL常用語句

and和or可以混用,and比or具有更高的優先順序,但盡量使用圓括號區分 自動過濾重複的資料owner,關鍵字distinct select distinct owner from pet 按照生日公升序排列,關鍵字order by select name,birth from pet order...