mysql高階查詢

2021-10-06 07:08:55 字數 2198 閱讀 2594

修改表名

alter table 表1 rename 表2

(修改的新錶名)

;

新增字段

alter table 表名 add 欄位名 varchar(20

) not null;

修改字段

alter table 表名 change name username varchar(20

) not null;

刪除字段

alter table demo3 drop password;
新增主外來鍵約束

*

*新增主鍵*

*alter table grade add constraint pk_gradeid primary key grade

(gradeid);*

*新增唯一約束*

*alter table grade add constraint `uk_gradename` unique key grade (gradename)

;show create table grade;

**新增外來鍵約束*

*alter table stuinfo add constraint `fk_gradeid` foreign key (gradeid) references grade

(gradeid)

;

字串連線

select concat (

'my'

,'sql');

select concat

(classhour-

10) from subject where subjectno=

1;

字串擷取

select substring

(identitycard,7,

8) from stuinfo;

字串替換

select insert

('aaasdjai',3

,2,'wo');

select insert

(email,1,

14,'[email protected]'

)from stuinfo where studentno=

1;

日期函式

-- 獲取系統日期

select now()

;select curdate()

,curtime()

;-- 前面年月日 後面幾分幾秒

-- 查詢第幾周

select week

(now()

);-- 返回當前的年份

select year

(now()

);-- 返回當前的月份

select month

(now()

);-- 返回當前日

select day

(now()

);-- 返回當前小時

select hour

(now()

);-- 返回分鐘

select minute

(now()

);-- 兩個日期的間隔

select datediff

(now()

,'2001-08-29'

);

子查詢

select studentname

from stuinfo

where studentno in (select studentno from result where subjectno =

(select subjectno from subject where subjectname=

'chinese'

and studenresult=55)

);

mysql高階查詢in MySQL高階查詢(一)

in 子查詢 巢狀查詢 重點是思路 為什麼要用in?in 在數值上相當於 但是它可以查詢到更多的符合條件的結果,等於號只可以查詢乙個結果 question 有兩種方法 第一種 使用子查詢替換表連線 使用 inner join 將表與表之間聯動,再將實現條件依次寫出來 第二種 採用子查詢 在where...

mysql 高階 查詢 MYSQL中的高階查詢

1.1.子查詢 1.1.1.在房屋型別中,如何找出比雙人間貴的所有房屋型別?找到雙人間的 根據第一步找到的 作為查詢條件去查滿足條件的房屋型別,利用where字句 子查詢是乙個巢狀在 select insert update 或 delete 語句或其他子查詢中的查詢 子查詢在where語句中的一般...

Mysql高階查詢

if expr1,expr2,expr3 如果 expr1 是true 則 if 的返回值為expr2 否則返回值則為 expr3。如 select if status 0 and status promotion 1,1,2 as status from my table ifnull expr1...