Mysql常用語句與函數(待續)

來源:互聯網
上載者:User

標籤:

-- 查詢語句
select class from stu_info where sid=1000000102;
select * from stu_info t where t.age=88; -- t是表的別名,多表查詢時比較方便
select * from atable a, btable b where a.aID = b.bID;
select * from stu_info t where t.age=99 or (t.age>20 and t.age <90);
select * from stu_info t where t.age between 10 and 99;
select * from stu_info t where t.hometown like ‘110%‘ ; -- %萬用字元,表示0到多個任一字元
select * from stu_info t where t.hometown like ‘_10%‘ ; -- _萬用字元,表示任意一個字元
select * from atable a where a.aID in (1,2);
select * from atable a where a.aID not in (1,2);
select * from atable a where a.aID in (select bID from btable);
select * from atable order by aID desc -- asc升序|desc降序,預設為asc
select name,count(*) as total from score_info group by name;
select * from stu_info limit 10; -- 查詢前十條,起始位置預設為0,且limit只能放到sql語句的最後
select * from stu_info limit 101,110; -- 查詢101-110條記錄

-- 更新語句
update stu_info t set t.class=‘ceshi‘ where t.age=88;
select * from stu_info t where t.class=‘ceshi‘;

-- 插入語句
insert into atable (aID,aNAME) values (8,‘test‘); -- 基本文法
insert into atable values (6,‘a20050111‘ ),( 7,‘a20050112‘); -- 不指定欄位,則由左向右依次插入值
insert into atable select * from btable; -- 前提是表結構一樣或是按欄位插入
insert into atable select * from btable on duplicate key update anum=99;-- 如果存在主鍵衝突,則跳過主鍵執行update操作
select * from atable;
select * from btable;

-- 刪除語句
test.rollback()
delete from atable where aID=8; -- 條件刪除,如果不加條件,則全表資料刪除,可復原
truncate table atable; -- 刪除全表資料,刪除效率要高於delete,但是刪除操作不可復原
drop table atable; -- 直接刪表

-- 修改表結構語句
desc score_info; -- 查詢表結構
alter table score_info rename to score; -- 修改表名
alter table score_info add sex char(2); -- 增加欄位
alter table score_info drop sex; -- 刪除欄位
alter table score_info modify sex varchar(2); -- 修改欄位類型,modify只可以修改欄位類型
alter table score_info change sex sosex char(2) -- 修改欄位名稱和欄位類型

-- 聯表查詢
select * from a left join b on a.aID = b.bID; -- 左串連,查詢出a表的全部記錄,b表合格記錄
select * from a right join b on a.aID = b.bID; -- 右串連,查詢出b表的全部記錄,a表合格記錄
select * from a inner join b on a.aID = b.bID; -- 內串連或相等串連,取交集
select * from a,b where a.aID = b.bID; -- 等價於內串連
select * from a union select * from b; -- 查詢a與b表的並集,結果去重
select * from a union all select * from b;
select * from b

-- 常用函數
select rand(); -- 該函數結果返回0-1之間的浮點型隨機數,不可有參數
select round(3.45); -- 該函數原型round(x,y),四捨五入方式返回最接近於參數x的數,其值保留到小數點後面y位,沒有y時預設為0
select round(3.45,1);
select truncate(5.56,1); -- 函數原型truncate(x,y),參數一個不能少,返回x值保留y位小數
select floor(9.88); -- floor(x)返回x的整數部分,採取直接截掉小數部分,同truncate
select floor(rand()*1000);
select curdate(); -- 返回當前日期,同current_date()
select CURTIME(); -- 返回目前時間,同current_time()
select now(); -- 返回當前日期時間
select now()+0; -- +0後會去掉時間分隔字元,返回一個時間數字串
select date_format(now(),‘%Y-%m-%d‘); -- 格式化時間
select ltrim(‘ apple‘) as ltrim; -- 去掉左邊空格
select rtrim(‘ apple ‘) as rtrim; -- 去右邊空格
select trim(‘ apple ‘) as trim; -- 去首尾空格

sum(col);avg(col);count(col);min(col);max(col); -- 彙總函式常用到group by的select查詢中

select length(‘apple‘); -- 返回字元創長度
select concat(123,‘apple‘,999); -- 拼接字串
select concat_ws(‘-‘,2015,07,08); -- 拼接字串,並以‘-‘作為分割符
select group_concat(anum) from a; -- 返回由一列值拼接組成的列表
select left(‘apple‘,2); -- 返回最左邊的2個字元;
select right(‘apple‘,2); -- 返回最右邊的2個字元
select lower(‘APPLE‘); -- 返回小寫
select upper(‘apple‘); -- 返回大寫
select reverse(‘apple‘); -- 返回倒序字串

Mysql常用語句與函數(待續)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.