排序:
SELECT * FROM `leave` order by id desc //desc降序 asc升序
插入:
INSERT INTO `leave` VALUES (4,4,4,4,4,4,4)
INSERT INTO `leave`(`id`, `dayNumber`, `reason`, `comment`, `name`, `state`, `mm`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7])
说明:这两种都可以,第一种必须把所有字段值都写上
更新(update)
update `leave` set id='4',name = '44',reason='44' where id = '4'
删除:
delete from `leave` where id='4'
得到几条数据
SELECT * FROM `leave` ORDER BY id desc limit 2
SELECT top 2 * from `leave`//没有成功
Like:
SELECT * FROM `leave` WHERE name like '_三'
SELECT * FROM `leave` WHERE name not like '_三' ORDER BY id desc
Sql通配符:
% 替代0个或多个字符
SELECT * FROM `leave` WHERE name like '%三'
_ 替代 一个字符
SELECT * FROM `leave` WHERE name like '_三'
[charlist] 字符列中任一单一字符 // 没成功
SELECT * FROM `leave` WHERE name like '[ms张]三'
[^charlist] or [!charlist] 不在字符列中的任一单一字符 // 没成功
In :
SELECT * FROM `leave` WHERE name in ('张三','3')
Between
SELECT * FROM `leave` WHERE reason between 'a' and 'c'
别名:
SELECT id as id_,name as name_ FROM `leave` WHERE 1
SELECT l.name,u.id,l.id FROM `user` as u,`leave` as l WHERE u.id=l.id
inner Join
SELECT * FROM `user` inner join `leave` on leave.id=user.id
Left join
SELECT * FROM `leave` left join `user` on user.id=leave.name
Right join
select * from `user` right join `leave` on leave.id = user.id
Union //合并,查询的列数必须相同 union all 是允许重复值
SELECT name FROM `leave`
union
select id from `user`
Mysql不支持 select * into 这个语句
创造一个表;
create table 目标表 as select * from 原表;
Insert into select
Insrt into user(name,id) select id,name from leave
创建数据库
Create database databasename;
创建 一张表 :
create table `subject` (
id int,
name varchar(255)
)
约束
NOT NULL - 指示某列不能存储 NULL 值。
UNIQUE - 保证某列的每行必须有唯一的值。
PRIMARY KEY - NOT NULL 和 UNIQUE 的结合。确保某列(或两个列多个列的结合)有唯一标识,有助于更容易更快速地找到表中的一个特定的记录。
FOREIGN KEY - 保证一个表中的数据匹配另一个表中的值的参照完整性。
CHECK - 保证列中的值符合指定的条件。
DEFAULT - 规定没有给列赋值时的默认值
create table `subject2` ( id int , name varchar(255), check (id>10) )
删除表:
Drop table tablename
删除数据库:
Drop database databasename
添加一列
alter table `subject` add mm int
删除 一列
alter table `subject` drop mm
Sql函数
求平均数
select avg(state) from `leave`
select avg(state) as avg from `leave`
COUNT() 函数NULL 不计入
SELECT count(`mm`) as number from `leave`
求最大值:带字符比较
select max(`name`) from `leave`
求最小值:带字符比较
select min(`name`) from `leave`
SUM() 函数
SUM() 函数返回数值列的总数。(如果此列是字符串,他把数字字符求和)
Select sum(state) from `leave`
sql语句学习
排序:
SELECT * FROM `leave` order by id desc //desc降序 asc升序
插入:
INSERT INTO `leave` VALUES (4,4,4,4,4,4,4)
INSERT INTO `leave`(`id`, `dayNumber`, `reason`, `comment`, `name`, `state`, `mm`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7])
说明:这两种都可以,第一种必须把所有字段值都写上
更新(update)
update `leave` set id='4',name = '44',reason='44' where id = '4'
删除:
delete from `leave` where id='4'
得到几条数据
SELECT * FROM `leave` ORDER BY id desc limit 2
SELECT top 2 * from `leave`//没有成功
Like:
SELECT * FROM `leave` WHERE name like '_三'
SELECT * FROM `leave` WHERE name not like '_三' ORDER BY id desc
Sql通配符:
% 替代0个或多个字符
SELECT * FROM `leave` WHERE name like '%三'
_ 替代 一个字符
SELECT * FROM `leave` WHERE name like '_三'
[charlist] 字符列中任一单一字符 // 没成功
SELECT * FROM `leave` WHERE name like '[ms张]三'
[^charlist] or [!charlist] 不在字符列中的任一单一字符 // 没成功
In :
SELECT * FROM `leave` WHERE name in ('张三','3')
Between
SELECT * FROM `leave` WHERE reason between 'a' and 'c'
别名:
SELECT id as id_,name as name_ FROM `leave` WHERE 1
SELECT l.name,u.id,l.id FROM `user` as u,`leave` as l WHERE u.id=l.id
inner Join
SELECT * FROM `user` inner join `leave` on leave.id=user.id
Left join
SELECT * FROM `leave` left join `user` on user.id=leave.name
Right join
select * from `user` right join `leave` on leave.id = user.id
Union //合并,查询的列数必须相同 union all 是允许重复值
SELECT name FROM `leave`
union
select id from `user`
Mysql不支持 select * into 这个语句
创造一个表;
create table 目标表 as select * from 原表;
Insert into select
Insrt into user(name,id) select id,name from leave
创建数据库
Create database databasename;
创建 一张表 :
create table `subject` (
id int,
name varchar(255)
)
约束
NOT NULL - 指示某列不能存储 NULL 值。
UNIQUE - 保证某列的每行必须有唯一的值。
PRIMARY KEY - NOT NULL 和 UNIQUE 的结合。确保某列(或两个列多个列的结合)有唯一标识,有助于更容易更快速地找到表中的一个特定的记录。
FOREIGN KEY - 保证一个表中的数据匹配另一个表中的值的参照完整性。
CHECK - 保证列中的值符合指定的条件。
DEFAULT - 规定没有给列赋值时的默认值
create table `subject2` ( id int , name varchar(255), check (id>10) )
删除表:
Drop table tablename
删除数据库:
Drop database databasename
添加一列
alter table `subject` add mm int
删除 一列
alter table `subject` drop mm
Sql函数
求平均数
select avg(state) from `leave`
select avg(state) as avg from `leave`
COUNT() 函数NULL 不计入
SELECT count(`mm`) as number from `leave`
求最大值:带字符比较
select max(`name`) from `leave`
求最小值:带字符比较
select min(`name`) from `leave`
SUM() 函数
SUM() 函数返回数值列的总数。(如果此列是字符串,他把数字字符求和)
Select sum(state) from `leave`