基於mysql的一些sql語法
Sql:
distinct:
select distinct * from tea;
Tea中每行的數據必有不同,若有兩行相同的,則只輸出一行
Limit:
select * from stu limit 1,3
從stu表的第二行開始,選取3行
Null:
select * from stu where name is null;
查詢name為空的數據
Regexp:
select * from stu where name regexp ‘.‘;
匹配正則表達式
^[0-9]:
表示匹配從0-9數字開始的字符串
[^0-9]:
表示匹配不含0-9數字的字符串
[0-9]:
表示匹配含有0-9數字的字符串
[0-9]$:
表示匹配以0-9數字結束的字符串
Concat:
select concat(name,‘(‘,id,‘)‘) from stu
將數據組裝成name(id)形式列出
Trim:
select concat(trim(name),‘(‘,id,‘)‘) from stu;
Name中的數據去除頭尾連續的空格
Ltrim:
select concat(ltrim(name),‘(‘,id,‘)‘) from stu;
去除name中開始時連續的空格
Rtirm:
select concat(rtrim(name),‘(‘,id,‘)‘) from stu;
去除name中結束時連續的空格
now():
select now();
返回當前時間
自然連接:
兩個表中都有例如name屬性的化,只顯示一個
Union:
select * from stu where name = ‘dj‘ union all select * from stu where name = ‘dd‘;
合並兩個查詢的數據集,union不顯示重復的數據,union all顯示重復的數據
全文本搜索:
創建表時需:
指明engine引擎為myisam,指明檢索的屬性note_text
通過match和against進行搜索匹配
和like相比,這個會返回一定的順序,匹配頻率等
必須聲明在 boolean mode下
優先級:
insert low_priority into stu values(null,‘d‘);
· LOW_PRIORITY關鍵字應用於DELETE、INSERT、LOAD DATA、REPLACE和UPDATE。
· HIGH_PRIORITY關鍵字應用於SELECT和INSERT語句。
· DELAYED關鍵字應用於INSERT和REPLACE語句。
LOW_PRIORITY :低優先級。如果您使用LOW_PRIORITY關鍵詞,則INSERT的執行被延遲,直到沒有其它客戶端從表中讀取為止。在讀取量很大的情況下,發出INSERT LOW_PRIORITY語句的客戶端有可能需要等待很長一段時間(甚至是永遠等待下去)。
DELAYED :延遲。如果您使用DELAYED關鍵字,則服務器會把待插入的行放到一個緩沖器中,而發送INSERT DELAYED語句的客戶端會繼續運行。如果表正在被使用,則服務器會保留這些行。當表空閑時,服務器開始插入行,並定期檢查是否有新的讀取請求。如果有新的讀取請求,則被延遲的行被延緩執行,直到表再次空閑時為止。
註意,目前在隊列中的各行只保存在存儲器中,直到它們被插入到表中為止。這意味著,如果您強行中止了mysqld(例如,使用kill -9)或者如果mysqld意外停止,則所有沒有被寫入磁盤的行都會丟失。
HIGH_PRIORITY:高優先級。如果您指定了HIGH_PRIORITY,同時服務器采用--low-priority-updates選項啟動,則HIGH_PRIORITY將覆蓋--low-priority-updates選項。
--low-priority-updates:mysqld命令行選項。set low_priority_updates = 1 指定更新使用低優先級(默認更新優先級高於查詢)。
Ignore:
update ignore stu set name = ‘dk‘ where id = ‘13‘;
即使發生錯誤,也要繼續更新
truncate table:
刪除並創建表,更快的用於刪除表中所有的數據
select last_insert_id();
得到最後一次插入的id
一個引擎的表不能引用另個引擎的表的外鍵
alter table:
更新表定義
更新視圖(創建或者替代視圖)
create or replace view s as select * from stu where id%2 = 0;
delimiter //
將結束符號改成 //
存儲過程:
mysql> create procedure stuName()
-> begin
-> select name
-> from stu;
-> end //
call stuName()//
調用存儲過程stuName
drop procedure if exists stuName//
如果stuname存在,則刪除存儲過程stuName
create procedure stuFun( out a decimal(8,2), out b decimal(8,2) ) begin select count(*) into a from stu; select avg(id) into b from stu; end//
將count(*)值傳給a , 將avg(id)值傳給b, decimal(8,2) 有效位8位,保留2位小數
call stuFun(@a,@b)//
調用stuFun這個存儲過程,將count(*)值傳給a , 將avg(id)值傳給b
select @a//
顯示a的結果,也就是count(*)的結果
create procedure stuSum( in num int, out sum decimal(8,2) ) begin select count(*) from stu where id = num into sum; end
傳入num值,得到sum值
call stuSum(1,@sum)//
調用存儲過程
Select @sum;
顯示sum的結果
create procedure stuPrint( in num int ) begin select name from stu where id = num; end//
傳入num值,根據num值來獲得用戶名
create procedure fun(
in num int, i
n b boolean,
out result decimal(8,2)
)
begin
declare total decimal(8,2) ;
declare xishu int default 2;
select count(*) from stu where id = num
into total;
if b then
select total*xishu into total;
end if;
select total into result;
end//
declare聲明變量
if b then … end fi; 如果b為真則。。
觸發器:
當執行update, delete ,insert時觸發一條sql語句
創建用戶:
create user dj identified by ‘123456‘;
重命名用戶:
rename user dj to dd;
刪除用戶:
drop user dj;
給用戶權限:
grant select on test.* to dj;
讓dj用戶可以對test數據庫所有的表進行查詢
修改用戶密碼:
set password for dj = password(‘123‘);
修改自己權限密碼
set password = password(‘123‘);
基於mysql的一些sql語法