MySQL之select查詢、function函數


一、select查詢

//查詢某張表所有數據 select * from temp; //查詢指定列和條件的數據 //查詢name和age這兩列,age等於22的數據 select name,age from temp where age = 22; //as對列重命名 //as可以省略不寫,如果重命名的列名出現特殊字符,如單引號,那就需要用雙引號引在外面 select name as '名稱' from temp; //給table去別名 select t.name Name from temp as t; //where條件查詢 >>=<<==<>都可以出現在where語句中 select from t where a > 2 or a>=3 or a<5 or a<=6 or a=7 or a<>0; //and 並且 //查詢名稱等於Jack並且年齡大於20的 select * from temp where age > 20 and name = 'jack'; //or或者 --滿足一個條件即可
select * from temp where name = 'jack' or name = 'jackson'; //between v and v2 --大於等於v且小於等於v2
select * from temp where age between 20 and 25; //in 查詢 --可以多個條件,類似於or --查詢id 在括號中出現的數據
select *from temp where id in (1, 2, 3); //like模糊查詢 --查詢name以j開頭的
select * from temp where name like 'j%'; --查詢name包含k的
select * from temp where name like '%k%'; --escape轉義,指定\為轉義字符,上面的就可以查詢name中包含“_”的數據
select * from temp where name like '\_%' escape '\'; //is nullis not null
--查詢為null的數據
select * from temp where name is null; --查詢不為null 的數據
select * from temp where name is not null; //order by
--排序,升序(desc)、降序(asc) --默認升序
select * from temp order by id; select * from temp order by id asc; --多列組合
select * from temp order by id, age; //not
select * from temp where not (age > 20); select * from temp where id not in(1, 2); //distinct去掉重復數據 select distinct id from temp; //多列將是組合的重復數據 select distinct id, age from temp; //查詢常量 select 5+2; select concat('a', 'bbb'); //concat函數,字符串連接 //concat和null進行連接,會導致連接后的數據成為null select concat(name, '-eco') from temp; //對查詢的數據進行運算操作 select age +2, age / 2, age - 2, age * 2 from temp where age - 2 > 22;

 二、函數

函數類似於存儲過程,只是調用方式不同

//創建函數 create function addAge(age int) returns int
    return age + 5; //使用函數: select addAge(age) from temp; //刪除函數 drop function if exists addAge; drop function addAge; //顯示創建語法 show create function addAge;

 

三、觸發器

觸發器分為insert、update、delete三種觸發器事件類型,還有after、before觸發時間

 

//創建觸發器 create trigger trg_temp_ins before insert
on temp for each row begin
insert into temp_log values(NEW.id, NEW.name); end
//刪除觸發器 drop trigger trg_temp_ins

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM