java學(xué)習(xí) sql操作常用語句

一、創(chuàng)建表:
表名:employee

create table employee  
(  
    id int,  
    name varchar(40),  
    sex varchar(4),  
    birthday date,  
    entry_date date,  
    salary decimal(8,2),  
    resume text  
);

二、增刪改查
(1)插入數(shù)據(jù)

insert into employee(id,name,sex,birthday,entry_date,salary,resume) values(1,'zhangsan','male','1993-03-04','2016-11-10','1000','i am a developer');  
/*可以省略表字段,但是必須插入全部字段*/  
insert into employee values(1,'zhangsan','male','1993-03-04','2016-11-10','1000','i am a developer'); 

(2)指定某列插入數(shù)據(jù)

insert into employee(id) values(6);  

(3)查看漢字時(shí)不亂碼

insert into employee(id,name) values(6,'張三');  
 /*告訴mysql客戶采用gb2312編碼*/  
  
    show variables like 'chara%';  
  
    set character_set_client=gb2312;  
  
    insert into employee(id,username) values('3','張三');  
  
     /*查看漢字時(shí)不亂碼*/  
  
       show variables like 'chara%';  
    set character_set_results=gb2312;  
    select * from employee;  

(4)刪除表數(shù)據(jù)

/*刪除表中名稱為’zs’的記錄*/  
delete from employee where name='zs';  
  
/*刪除表中所有記錄*/  
delete from employee;  
  
/*使用truncate刪除表中記錄(快速刪除表中所有數(shù)據(jù),保留表的數(shù)據(jù)結(jié)構(gòu))*/  
truncate table employee;   

(5)修改表中數(shù)據(jù)

/*將所有員工薪水修改為5000元*/  
update employee set salary=5000;  
  
/*將姓名為’zs’的員工薪水修改為3000元*/  
update employee set salary = 3000 where name='zs';  
  
/*將姓名為’aaa’的員工薪水修改為4000元,job改為ccc*/  
update employee set salary = 4000,job='ccc' where name='aaa';  
  
/*將lisi的薪水在原有基礎(chǔ)上增加1000元*/  
update employee set salary = salary+1000 where name='lisi';  

(6)查詢表中數(shù)據(jù)

/*查詢表中所有學(xué)生的信息*/  
select id,name,chinese,english,math from student;  
  
/*查詢表中所有學(xué)生的姓名和對應(yīng)的英語成績*/  
select name,english from student;  
  
/*過濾表中重復(fù)數(shù)據(jù)*/  
select distinct english from student;  
  
在所有學(xué)生分?jǐn)?shù)上加10分特長分*/  
select name,(chinese+english+math)+10 from student;  
  
/*統(tǒng)計(jì)每個(gè)學(xué)生的總分*/  
select name,(chinese+english+math) from student;  
  
/*使用別名表示學(xué)生分?jǐn)?shù)*/  
select name,(chinese+english+math) as 總分 from student;  
  
/*使用別名表示學(xué)生分?jǐn)?shù),可以不用as*/  
select name,(chinese+english+math) 總分 from  student;  
  
/*查詢姓名為張三的學(xué)生成績*/  
select * from student where name='張三';  
  
/*查詢英語成績大于90分的同學(xué)*/  
select * from student where english>'90';  
  
/*查詢總分大于200分的所有同學(xué)*/  
select name,(chinese+english+math) 總分 from student where chinese+english+math>200;  
  
/*查詢英語分?jǐn)?shù)在 80-90之間的同學(xué)*/  
select * from student where english>=80 and english=<90;  
select * from student where english between 80 and 90;  
  
/*查詢數(shù)學(xué)分?jǐn)?shù)為89,90,91的同學(xué)*/  
select * from student where math=89 or math=90 or math=91;  
select * from student where math in(89,90,91);  
  
/*查詢所有姓李的學(xué)生成績*/  
select * from student where name like '李%';  
select * from student where name like '李_';  
  
/*查詢數(shù)學(xué)分>80,語文分>80的同學(xué)*/  
select * from student where math>80 and chinese>80;  
select * from student where chinese is null;  
  
/*對數(shù)學(xué)成績排序(降序:從高到低,desc)后輸出。*/  
select name,math from student order by math desc;  
  
/*對數(shù)學(xué)成績排序(升序:從低到高,asc)后輸出。*/  
select name,math from student order by math asc;  
  
/*對總分排序后輸出,然后再按從高到低的順序輸出*/  
select name,math+english+chinese from student order  by math+english+chinese desc;  
  
/*對姓李的學(xué)生成績排序輸出*/  
select name,math+english+chinese from student where name like '李%' order by math+english+chinese desc;  
  
/*統(tǒng)計(jì)一個(gè)班級共有多少學(xué)生*/  
select count(*) from student;  
select count(id) from student;  
  
/*統(tǒng)計(jì)數(shù)學(xué)成績大于80的學(xué)生人數(shù)*/  
select count(*) from student where math>80;  
  
/*統(tǒng)計(jì)總分大于250的人數(shù)*/  
select count(*) from student where math+english+chinese>250;  
  
/*細(xì)節(jié) null不能被count*/  
select count(chinese) from student;  
  
/*統(tǒng)計(jì)一個(gè)班級數(shù)學(xué)總成績*/  
select sum(math) from student;  
  
/*統(tǒng)計(jì)一個(gè)班級語文、英語、數(shù)學(xué)各科的總成績s*/  
elect sum(math),sum(english),sum(chinese) from student;  
  
/*統(tǒng)計(jì)一個(gè)班級語文、英語、數(shù)學(xué)的成績總和*/  
select sum(math+english+chinese) from student;  
  
/*統(tǒng)計(jì)一個(gè)班級語文成績平均分*/  
select sum(chinese)/count(*) from student;  
  
/*求一個(gè)班級數(shù)學(xué)平均分*/  
select avg(math) from student;  
  
/*求一個(gè)班級總分平均分*/  
select avg(math+english+chinese) from student;  
  
/*求班級最高分和最低分(數(shù)值范圍在統(tǒng)計(jì)中特別有用)*/  
select max(math+english+chinese) from student;  
select min(math+english+chinese) from student;  
  
/*對訂單表中商品歸類后,顯示每一類商品的總價(jià)*/  
select product,sum(price) from orders group by product;  
  
/*查詢購買了幾類商品,并且每類總價(jià)大于100的商品*/  
select product,sum(price) from orders group by product having sum(price)>100;  
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容