常用mysql語句積累

2021-08-07 11:06:34 字數 2299 閱讀 1910

檢視當前的連線數

show status like '%threads_connected%';
檢視系統變數及其值

show variables
檢視myql的當前時間,使用者,版本

select now(),user(),version();
檢視全域性系統變數

show global variables
狀態變數

狀態變數可以使我們及時了解mysql伺服器的執行狀況,可以使用show status語句檢視。狀態變數和相同變數似,也分為全域性級和會話級,show status也支援like匹配查詢,比較大的不同是狀態變數只能由mysql伺服器本身設定和修改,對於使用者來說是唯讀的,不可以通過set語句設定和修改它們。
複製表

1. 使用 show create table 命令獲取建立資料表(create table) 語句,該語句包含了原資料表的結構,索引等。

2. 複製以下命令顯示的sql語句,修改資料表名,並執行sql語句,通過以上命令 將完全的複製資料表結構。

3. 如果你想複製表的內容,你就可以使用 insert into ... select 語句來實現。

使用 group by 來讀取資料表中不重複的資料:

mysql>

select last_name, first_name

-> from person_tbl

->

group

by (last_name, first_name);

如果你想刪除資料表中的重複資料,你可以使用以下的sql語句:
mysql> create table tmp select last_name, first_name, ***

-> from person_tbl;

-> group by (last_name, first_name, ***);

mysql> drop table person_tbl;

mysql> alter table tmp rename to person_tbl;

獲取當前是時間及日期

curdate()  獲取單前的日期     2017-09-05

now() 獲取當前的日期時間 2017-09-05 12:59:36

當前日期作加減法

adddate(curdate(),interval -1 day)

//減法

adddate(curdate(),interval 1 day)

//加法

date_add(createtime, interval 15 minute)

資料表中新增乙個偽列

這裡的type就是做出來的偽列,原表中並沒有
select '1' as

type,url,resource,main

title,date_format

(s.c_time,'%y-%m-%d %h:%i') createtime from show_notice_list

執行有則更新,無則新增

insert into user_message_setting (...) values (...) on duplicate key update...
判斷時間在乙個時間段之間 可以採用 between and

date_format(now(),'%y-%m-%d %h:%i:%s')  between deal_time and lost_time

MySql常用函式積累

mysql檢視表結構 select column name,data type,character maximum length,column comment from information schema.columns where table schema db name and table n...

常用mysql語句 常用MySql語句

新建資料表 drop table if exists ga game way create table ga game way id int 11 unsigned not null auto increment comment id primary key id using btree,主鍵 un...

SQL SQL語句積累

實際案例 例 結果集中,name 胡聰 age 23 性別 男,想要輸出diy欄位 select su.name su.age su.concat 姓名 su.name,年齡 su.age,性別 su.as memo from sys user輸出 name age memo 胡聰23 男姓名胡聰年...