MySql 常用命令
1. 查询数据(Select)
基本select语句:
一个基本的select语句可分解成三个部分:查找什么数据(select)、从哪里查找(from)、查找的条件是什么(where)。
1.1 语法格式
select 语句的一般格式如下:
select <目标列表达式列表>[into 新表名]from 表名或视图名[where <条件>][group by <分组表达式>][having <条件>][order by <排序表达式>[ASC|DESC]]
1.2 查询指定字段
查询表中所有列
在select语句指定列的位置上使用号时,表示查询表的所有列。
模板:select from tb_name;select * from student
查询表中指定的列
查询多列时,列名之间要用逗号隔开。
模板: select <字符型字段>,<字符型字段> … from tb_name;SELECT sex,username FROM student
指定查询结果中的列标题
通过指定列标题(也叫列别名)可使输出结果更容易被人理解。
模板:select <字段> as 别名,<字段2> as 别名2 from bt_name;SELECT sex as aa,username as bb FROM student
1.3 查询满足条件的行:
用where子句实现条件查询
通过where子句实现,该子句必须紧跟在From子句之后。
格式为:select * from 表名 where <条件>;
说明:在查询条件中可使用以下运算符或表达式:
运算符 运算符标识
比较运算符 <=,<,=,>,>=,!=,<>,!>,!<
范围运算符 between… and,not between… and
列举运算符 in,not in
模糊匹配运算符 like,not like
空值运算符 is null,is not null
逻辑运算符 and,or,not使用比较运算符:
模板:select * from tb_name where <字段> >= n ;SELECT * FROM student where age > 13
指定范围:
用于指定范围的关键字有两个:between…and和 not between…and。
格式为:select * from tb_name where <字段> [not] between <表达式1> and <表达式2>;
其中:between关键字之后的是范围的下限(即低值), and关键字之后的是范围的上限(即高值) 用于查找字段值在(或不在)指定范围的行。
SELECT * FROM student where age between 13 and 24// 查询age字段的值 在13到24 范围的数据SELECT * FROM student where age node between 13 and 24// 查询age字段的值 不在13到24 范围的数据
使用列举:
使用in关键字可以指定一个值的集合,集合中列出所有可能的值,当表达式的值与集合中的任一元素个匹配时,即返回true,否则返回false。
模板:select * from tb_name where <字段> [not] in (值1,值2,…,值n);SELECT * FROM student where age in (23,43)// 查询age字段的值 是23 或者43 的数据SELECT * FROM student where age not in (23,43)// 查询age字段的值 不是23 或者43 的数据
使用通配符进行模糊查询:
可用like 子句进行字符串的模糊匹配查询,like子句将返回逻辑值(true或False)。
模板: select * from tb_name where <字段> [not] like <匹配串>;其含义是: 查找指定字段值与匹配串相匹配的记录。匹配串中通常含有通配符%。 其中: %:代表任意长度(包括0)的字符串
SELECT * FROM student where username like "%小%"// 查询username字段中包含`小`字符的
使用null的查询
当需要判定一个表达式的值是否为空值时,使用 is null关键字。
当不使用not时,若表达式的值为空值,则返回true,否则返回false;当使用not时,结果刚好相反。
模板:select * from tb_name where <字段> is [not] null;SELECT * FROM student where username is null// 查询username字段为空的数据SELECT * FROM student where username is not null// 查询username字段不为空的数据
多重条件查询:使用逻辑运算符
逻辑运算符and(与:两个条件都要满足)和or(或:满足其中一个条件即可)可用来联接多个查询条件。and的优先级高于or,但若使用括号可以改变优先级。
模板:select * from tb_name where <字段> = ‘values’ and <字段> > n;SELECT * FROM student where username like "%小%" and sex = "女"// 查询username 字段值包含"小" 并且 sex字段值为"女"SELECT * FROM student where username like "%小%" or age >= 40// 查询username 字段值包含"小" 或者 age字段 大于等于 40
1.4 对查询结果排序
order by子句可用于对查询结果按照一个或多个字段的值(或表达式的值)进行升序(ASC)或降序(DESC)排列,默认为升序。<br /> 格式:order by {排序表达式[ASC|DESC]}[,...n];<br /> 其中:排序表达式既可以是单个的一个字段,也可以是由字段、函数、常量等组成的表达式,或一个正整数。<br /> 模板:select * from tb_name order by <排序表达式> <排序方法>;
SELECT * FROM student where username like "%小%" or age >= 40 order by id DESC// 将查询的结果按照id 字段的降序排序
2、插入数据(Insert)
insert into 语句用于向表格中插入新的行:
2.1 插入数据
第二种形式需要指定列名及被插入的值:
模板:insert into tb_name (column1,column2,column3,…) values (value1,value2,value3,…);INSERT INTO student (username,age,sex,classname) VALUES ("张三",12,"男",[1,3])
3.、修改数据(Update)
Update 语句用于修改表中的数据。
格式:update tb_name set 列名称 = 新值 where 列名称 = 某值;UPDATE student SET username = 张三 WHERR id = 100006
4.、删除数据(Delete)
4.1 删除单行数据
格式:delete from tb_name where 列名称 = 某值;
delete from student where id = 100006
4.2 删除所有行
可以在不删除表的情况下删除所有的行。这意味着表的结构、属性和索引都是完整的:
格式:delete * from tb_name 或 delete from tb_name;
