国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

mysql 基本操作記錄語句

2024-07-24 12:37:38
字體:
來源:轉載
供稿:網友

本文章來給大家介紹一下很簡單的mysql基于語句操作命令,包括創建,刪除,修改表及查詢與插入數據到mysql中的一些方法.

以下以數據庫”ceshi”為例.

1、連接數據庫

mysql -u username -p password

2、創建/刪除數據庫

創建:create database ceshi;

刪除:drop database ceshi;

3、創建/刪除數據表

創建:create table students (sid int(10) auto_increment primary key,name varchar(255),course varchar(255),score int(10)) ;

刪除:drop table students;

設置數據表編碼:alter table `students` default character SET utf8 collate utf8_general_ci;

4、插入數據

單條插入:insert into students (name,course,score) values(value1,value2,value3);

多條插入:insert into students (name,course,score) select value1[0],value1[1],value1[2] union select value2[0] ,value2[1],value2[2] union……

從另外的一張表中讀取多條數據添加到新表中:insert into students(col1,col2,col3) select a,b,c from tableA ;

從其他的多張表中讀取數據添加到新表中:insert ioto tableName(col1,col2,col3)  select a,b,c from tableA where a=1  union all select a,b,c from tableB where a=2  

上邊代碼中的union all如果換成union,則相同記錄只插入一次,不會重復插入,上邊代碼中的into都可以省略.

5、order by語句

select * from students order by score(asc); 從低往高排,默認,asc可省去.

select * from students order by score desc; 從高往低排

6、group by語句

select * from students group by course; 查詢數據按課程分組,只顯示查詢到的第一條.

select * from students group by course order by socre; order by 必須在 group by之后,group by 比order by先執行,order by不會對group by 內部進行排序,如果group by后只有一條記錄,那么order by 將無效。要查出group by中最大的或最小的某一字段使用 max或min函數。

簡單的查增刪改

  1. --查看學生表的全部數據 
  2. select * from studio 
  3. --插入一個新的學生信息 
  4. insert into studio(st_name,st_sex,st_age,st_add,st_tel) values("黃蘭淇",0,36,'南充','13943943334'
  5. --查看class全部數據 
  6. select * from class 
  7. --向class表增加兩條條數據 
  8. insert into class(cl_class,cl_coding,cl_o_time,cl_remark) values('新電實訓班','GXA-ncs-001','2008-03-11','都是很優秀的朋友'
  9. insert into class(cl_class,cl_coding,cl_o_time) values('阿壩師專實訓班','GXA-ABSZ-001','2008-03-11'
  10. --更新一條的數據 條件的重要性 
  11. update class set cl_remark='真的是不錯' where cl_id=5 
  12. --刪除一條數據 條件的重要性 
  13. delete from class where cl_id=7 
  14. --修改列標題 
  15. select cl_id as '班級主鍵',cl_class as '班級名稱' from class 
  16. select 名字=st_name from studio 
  17. --使用文字串 
  18. select '名字是:',st_name from studio 

條件稍微復雜點的查增刪改:

  1. --主要涉及到 or and not between in like > < = !> !< != <> () <= >= is null is not null 
  2.   --查詢cl_id 大于 1 的所有信息 
  3.   select * from class where cl_id>1 
  4.   --使用 or 
  5.   select * from class where cl_id<>10 or cl_class='百杰一班' 
  6.   --使用and 
  7.   select * from class where cl_id<>10 and cl_class='百杰一班' 
  8.   --使用like 和 % 
  9.   select * from class where cl_class like '百杰%' 
  10.   select * from class where cl_remark like '%上午%' 
  11.   --使用 between 
  12.   select * from class where cl_id between 3 and 5 
  13.   --使用 between 配合上 not 
  14.   select * from class where cl_id not between 3 and 5 
  15.   --使用 is not null 
  16.   select * from class where cl_remark is not null 
  17.   --使用 in  Vevb.com 
  18.   select * from class where cl_class in('千星一班','百杰二班'

使用數學運算符:

  1. --主要涉及到 + = * 
  2. --查詢Java相關課程分別要上多少周 按照每周5天,每天6節課來計算 
  3. select '結果'=co_num/5/6 from course where co_name in ('Java基礎','Java項目入門'

使用匯總函數:

  1. --涉及到COUNT SUM AVG MAX MIN 
  2. --查詢課時數小于50的課程一共有多少門 
  3. select count(*) from course where co_num<50 
  4. --查詢所有課程一共多少課時 
  5. select sum(co_num) from course 
  6. --計算全部課時費,假設每節課50塊錢 
  7. select sum(co_num)*50 from course 
  8. --查詢課時最少的課程 
  9. select min(co_num) from course 
  10. --查詢課時最多的課程 
  11. select max(co_num) from course 
  12. --查詢平均每門課多少課時 
  13. select avg(co_num) from course 

聚集函數:

最大值:max() 最小值 min()  平均值avg()

求和:sum() 匯總:count ()

如:求每個部門的基本工資平均值

select 部門,avg(基本工資)  as 部門基本工資 from 員工表 group by 部門

顯示平均基本工資大于3000的部門

Select  部門,avg(基本工資) from 員工表 group 部門 where avg(基本工資)>3000

此句錯誤,SQL規定在分組中使用條件不能用 Where 而是用 having

Select  部門,avg(基本工資) from 員工表 group by  部門 having avg(基本工資)>3000

八、多表查詢:

一個數據庫中的多個表,存在一定的聯系,怎么樣正常的顯示這么表的信息?

現在有三個表:

  1. yg 
  2. Name  sex  age   
  3. 宋洋  男   20   
  4. 馬冬旭 女   40  
  5.  
  6.  Gs 
  7. Name   title      date       單位 
  8. 宋洋   AD詳解 2006-11-10  清華大學 
  9. 馬冬旭 linux    2005-01-01   人民大學 
  10.  
  11.  dz 
  12. 單位     地址 
  13. 清華大學  五道口 
  14. 人民大學   黃莊 

第一種方法稱為:交叉連接,在SQL SERVER中又稱為笛卡爾乘積,但是要注意的默認生成的記錄總數是兩表記錄之積.

select * from yg,gs;

select * from yg,gs where yg.name=gs.name;

這才是我們想要的記錄.

第二種方法是用join連接:

內連接:select * from yg join gs on yg.name=gs.name

左外連接,右外連接,但沒有全外連接.

九、聯合:

除了連接,mysql4.0以上的版本還支持UNION運算符,它用來把多個select查詢號的輸出連接成一個單獨的結果集,大多數情況下,這個運算符用來把查詢產生的結果集添加到不同的表,同時創建包括所有結果的一個單獨表,比如面試的時候問你,有兩個表,字段信息一樣,讓你用一條語句把兩個表的信息組合為一個單獨的表.

為了說明UNION運算符的使用方法,我們舉一個例子,現在有兩個表,分別存放的是男同學信息和女同學信息,如果用一個語句將所有同學的信息顯示出來.

  1. mysql> select * from nan; 
  2. +--------+-------+ 
  3. name   | score | 
  4. +--------+-------+ 
  5. | 彭聰留 |    80 | 
  6. | 費優優 |    81 | 
  7. | 曲權   |    82 | 
  8. +--------+-------+ 
  9. rows in set (0.00 sec) mysql> select * from nv; 
  10. +------+-------+ 
  11. name | score | 
  12. +------+-------+ 
  13. | 彭紅 |    80 | 
  14. | 費紅 |    81 | 
  15. | 曲紅 |    82 | 
  16. +------+-------+ 
  17. rows in set (0.00 sec) 
  18. mysql> select * from nan union select * from nv; 
  19. +--------+-------+ 
  20. name   | score | 
  21. +--------+-------+ 
  22. | 彭聰留 |    80 | 
  23. | 費優優 |    81 | 
  24. | 曲權   |    82 | 
  25. | 彭紅   |    80 | 
  26. | 費紅   |    81 | 
  27. | 曲紅   |    82 | 
  28. +--------+-------+ 
  29. rows in set (0.00 sec) 

那如果有三個表怎么辦?也是一樣的操作!

但注意的是如果兩個表的某條記錄信息完全一致,則只顯示為一條,如果想顯示全部記錄則在union后 加 all.

mysql> select * from nan union all  select * from nv;

如果面試官又問你,如果想把顯示的信息保存到一個表中怎么辦?

mysql> create table 表名 select 語句;

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陇川县| 静安区| 揭东县| 辛集市| 昌邑市| 宁武县| 綦江县| 佛山市| 连平县| 博白县| 饶阳县| 德阳市| 田林县| 县级市| 女性| 南靖县| 黄浦区| 鹤峰县| 西平县| 新平| 怀集县| 阿拉尔市| 贵德县| 鄄城县| 大渡口区| 白水县| 奇台县| 丰台区| 韶山市| 禹城市| 济阳县| 故城县| 大邑县| 南澳县| 兰坪| 浑源县| 遵化市| 泰来县| 手游| 北海市| 德保县|