刪除一個表:函數
drop table if exists 表名;排序
在表中插入行:ci
Insert into 表名 values(, , ,)rem
建立表:it
Create table 表名(table
Id int(10) primary key auto_increment, // auto_increment自增的意思select
Name varchar(10),im
Age int unsigned,查詢
Height decimal(5,2))tab
刪除行:
Delete from 表名 where ……
Tinyint類型 佔用一個字節,8個bit,範圍是0-255,
Int類型 佔用四個字節
查詢表:
Select * from 表名 查詢表中全部字段
Select name,age from 表名 從表中查詢name,age列
Select name as 別名 from 表名 從表中查詢name列,而且爲name列起一個別名
Select distinct sex from 表名 從表中查詢sex列,只留下不重複的,distinct:不重複
Select name from 表名 where…… 根據條件查詢表中name字段
Select * from 表名 where name like 「孫%」 模糊查詢,查找表中name爲孫X或者孫XX或者孫XXX等等的全部字段
Select * from 表名 where name like 「孫_ _」 查找姓名叫孫XX的全部字段
Select * from 表名 where name in (「老王」,」老李」,」老孫」) 查找姓名爲老王、老李、老孫的字段
Select * from 表名 where age between 18 and 20 查找年齡在18到20歲之間的全部字段
Select * from 表名 where age is null 查詢沒有年齡的全部字段
排序:
Select * from 表名 order by age 按照age排序,默認升序(asc)
Select * from 表名 order by age desc 按照age排序,降序排
聚合函數:
Select count(*) from 表名 查詢總行數
Select count(age) from 表名 查詢age列的總行數,不包含null
Select agv(age) from 表名 查詢平均年齡
Select sex,count(*) from 表名 group by sex having sex=’男’ 查詢性別男女分別有多少人,group by是分組,以性別分組,having相似where,後面是條件
Select * from 表名 limit startNum,count,好比 select* from 表名 limit 0,3 從表中第1行開始,查詢三行。
等值鏈接:
Select * from 表1,表2 where 表1.列名 = 表2.列名
內鏈接:
Select * from 表1 inner join 表2 on 表1.字段 = 表2.字段
Select * from 表1 join 表2 on 鏈接條件,
表2 join 表3 on 鏈接條件;
(後面還能夠跟where + 條件 )
左鏈接,join換成left join
右鏈接,join換成right join