本文章來給大家介紹一下很簡單的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函數。
簡單的查增刪改
- --查看學生表的全部數據
- select * from studio
- --插入一個新的學生信息
- insert into studio(st_name,st_sex,st_age,st_add,st_tel) values("黃蘭淇",0,36,'南充','13943943334')
- --查看class全部數據
- select * from class
- --向class表增加兩條條數據
- insert into class(cl_class,cl_coding,cl_o_time,cl_remark) values('新電實訓班','GXA-ncs-001','2008-03-11','都是很優秀的朋友')
- insert into class(cl_class,cl_coding,cl_o_time) values('阿壩師專實訓班','GXA-ABSZ-001','2008-03-11')
- --更新一條的數據 條件的重要性
- update class set cl_remark='真的是不錯' where cl_id=5
- --刪除一條數據 條件的重要性
- delete from class where cl_id=7
- --修改列標題
- select cl_id as '班級主鍵',cl_class as '班級名稱' from class
- select 名字=st_name from studio
- --使用文字串
- select '名字是:',st_name from studio
條件稍微復雜點的查增刪改:
- --主要涉及到 or and not between in like > < = !> !< != <> () <= >= is null is not null
- --查詢cl_id 大于 1 的所有信息
- select * from class where cl_id>1
- --使用 or
- select * from class where cl_id<>10 or cl_class='百杰一班'
- --使用and
- select * from class where cl_id<>10 and cl_class='百杰一班'
- --使用like 和 %
- select * from class where cl_class like '百杰%'
- select * from class where cl_remark like '%上午%'
- --使用 between
- select * from class where cl_id between 3 and 5
- --使用 between 配合上 not
- select * from class where cl_id not between 3 and 5
- --使用 is not null
- select * from class where cl_remark is not null
- --使用 in Vevb.com
- select * from class where cl_class in('千星一班','百杰二班')
使用數學運算符:
- --主要涉及到 + = *
- --查詢Java相關課程分別要上多少周 按照每周5天,每天6節課來計算
- select '結果'=co_num/5/6 from course where co_name in ('Java基礎','Java項目入門')
使用匯總函數:
- --涉及到COUNT SUM AVG MAX MIN
- --查詢課時數小于50的課程一共有多少門
- select count(*) from course where co_num<50
- --查詢所有課程一共多少課時
- select sum(co_num) from course
- --計算全部課時費,假設每節課50塊錢
- select sum(co_num)*50 from course
- --查詢課時最少的課程
- select min(co_num) from course
- --查詢課時最多的課程
- select max(co_num) from course
- --查詢平均每門課多少課時
- 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
八、多表查詢:
一個數據庫中的多個表,存在一定的聯系,怎么樣正常的顯示這么表的信息?
現在有三個表:
- yg
- Name sex age
- 宋洋 男 20
- 馬冬旭 女 40
- Gs
- Name title date 單位
- 宋洋 AD詳解 2006-11-10 清華大學
- 馬冬旭 linux 2005-01-01 人民大學
- dz
- 單位 地址
- 清華大學 五道口
- 人民大學 黃莊
第一種方法稱為:交叉連接,在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運算符的使用方法,我們舉一個例子,現在有兩個表,分別存放的是男同學信息和女同學信息,如果用一個語句將所有同學的信息顯示出來.
- mysql> select * from nan;
- +--------+-------+
- | name | score |
- +--------+-------+
- | 彭聰留 | 80 |
- | 費優優 | 81 |
- | 曲權 | 82 |
- +--------+-------+
- 3 rows in set (0.00 sec) mysql> select * from nv;
- +------+-------+
- | name | score |
- +------+-------+
- | 彭紅 | 80 |
- | 費紅 | 81 |
- | 曲紅 | 82 |
- +------+-------+
- 3 rows in set (0.00 sec)
- mysql> select * from nan union select * from nv;
- +--------+-------+
- | name | score |
- +--------+-------+
- | 彭聰留 | 80 |
- | 費優優 | 81 |
- | 曲權 | 82 |
- | 彭紅 | 80 |
- | 費紅 | 81 |
- | 曲紅 | 82 |
- +--------+-------+
- 6 rows in set (0.00 sec)
那如果有三個表怎么辦?也是一樣的操作!
但注意的是如果兩個表的某條記錄信息完全一致,則只顯示為一條,如果想顯示全部記錄則在union后 加 all.
mysql> select * from nan union all select * from nv;
如果面試官又問你,如果想把顯示的信息保存到一個表中怎么辦?
mysql> create table 表名 select 語句;
新聞熱點
疑難解答