Mysql語句練習記錄

使用的sql圖形軟件:SQLyogEntsql

使用的數據庫:MYSQL5.7數據庫

軟件地址:函數

連接:https://pan.baidu.com/s/1lajyXaSnmrO1v5v987NOoA
提取碼:i3a4
spa

-------------------------------------------------------------------------------------------------------------------------------------排序

//建立名爲student的數據庫get

create database student;產品

//顯示數據庫是否建立成功io

show databases;table

//跳轉到本身想要作修改的表,指定操做的數據庫名爲student效率

use student;

//建立stu表,在建立時指定屬性和屬性類型,並給於這個屬性指定大小

create table stu(id int(4),name varchar(12),age int(10),sex char(2),birthday date);

//顯示是否建立表成功

show tables;

//顯示數據庫stu表的基礎信息

desc stu;

//插入數據,varchar和char還有date類型數據使用‘’括起來

insert into stu values(1,'we',11,'n','2019-11-12');

//插入數據有兩種方式,一種是指定屬性插入和所有屬性插入

insert into stu(id,name) values(1,'ju');

insert into stu values(2,'w',11,'n','2019-11-12'),(3,'w',11,'n','2019-11-12'),(4,'w',11,'n','2019-11-12');//連着插入三條數據

//刪除student數據庫

drop database student;

//刪除stu表

drop table stu;

//修改數據庫表的信息

update stu set name='jing' where age=11;

update  stu set name='wan',age=18 where id=1;

//刪除

delete from stu where id=1;

delete from stu where name='li' and age=18;

//查詢

select * from product;//查詢全部

select name,price from product;

select name,price+10 from product;//查詢全部價格加十後的顯示結果

select p.name,p.price from product AS p;//給表起別名,多用於多表查詢

select name,price+10 AS "產品的新價格" from product;//給列起別名

select * from product where name="華爲電腦001";//查詢條件爲name="華爲電腦001"的所有記錄

select *from product where price!=23;//價格不等於23的全部記錄

select *from product where price>23 and price<100;//查找價格23到100之間的記錄

select * from product where price between 23 and 100;//查找價格23到100之間的記錄,包含23

select * from product where price = 23 or price=100;//價格等於23或100的全部記錄

select * from product where price in(23,100);//等同於上一句

     //碰到關鍵字在輸入的名稱左邊加頓號

select distinct type from product;//查找全部type並使用distinct去除重複

select * from product where type is null;//查詢出沒有分類的全部記錄

select * from product where type is not null;//查詢出有分類的全部記錄

select * from product order by price asc;//按照價格的大小升序排序 

select * from product order by price desc;//按照價格的大小降序排序

//聚合函數

select count(*) AS "總數" from product;//統計有多少條記錄,並起個別名(效率低不建議使用)

select count(1) as "總數" from product;//建議這樣使用 1 表明只遍歷統計下標爲1的屬性

select sum(price) from product;//查詢出全部的價格總和

select max(price) from product;//查詢價格最高

select min(price) from product;//查詢最低價格

select avg(price) from product;//查詢價格的平均值

select avg(price) as '平均值',min(price) as '最小值',max(price) as '最大值' from product;

select avg(price) as '平均值',min(price) as '最小值',max(price) as '最大值' ,count(1) as '總記錄數'from product;

模糊查詢

select *from product where name like '%電%';//%表明匹配一個或者多個字符,_ 只匹配一個字符

分組操做

select * from product group by type;//根據type進行分組,分組後重復會被去掉

分組後進行過濾,篩選

select * from product group by type having type is not null;

相關文章
相關標籤/搜索