@ mysql基础
1.通过mysql自带服务端(只限root用户)
2.cmd进入mysql命令:mysql 【-h localhost -P 3306 -】u 用户名 -p
停止exit
显示数据库:show databases;
打开一个库 : use 库名;
显示一个库有哪些表: show tables 【from 库名】;
查看库: select database();
建表:
create table 表名(列名 列类型,。。。);
描述一个表: desc 表名;
换行: 关键字和变量都换行
注释: #注释 – 注释
多行注释: /* 注释 */
DQL query 数据查询语言
DML manipulation 数据操作语言(增、删、改)
DDL define 数据定义语言
TCL transaction control 事务控制语言
@ 进阶1 简单查询
select
- select可以为:表中的字段(后边必须from表名)、常量值、表达式、函数
2.查询的结果为虚拟的表格
3.select后的变量与语句单词重复,使用着重号(·)
4.起别名:select 变量 【as】 别名
eg: 查询salary,显示结果为 out put
select salary as “out put” from employees;
5.去重
eg: select distinct 变量 from 表名;
不能出现 select distinct a, distict b from…,有的去的多有的去的少会报错
6.mysql中的加号只有运算符功能
select 'john'+90
select null+10
加号连接,如果其中有一个为字符型,试图转为数值型,若转换失败,将字符型记为0;如果其中一方为null,则结果为null。
拼接用 concat(‘a’,‘b’)
eg:显示表中的列,其中一列有null值
select
concat(first_name,',',last_name,',',ifnull(commission_pct,0))
from
employees;
@进阶2 条件查询
select
查询列表 3
from
表名 step1
where
筛选条件 2
条件分类:
1.条件表达式筛选 条件运算符:> < = != <>
2.按逻辑表达式. 逻辑运算符 and(&&) or(||) not(!)
3. 模糊查询 like / between / in / is null
eg: 查询部门编号不在90至100间,或工资高于15000的
select
*
from
employees
where
not(department_id>90 and department_id<100) or salary>15000;
字符值数据用单引号引
like
一般和通配符搭配使用 :%任意多个字符(包含0个);_一个通配符
'%'无法匹配出null值
like可以判断数值型或字符型
eg:查询第二个字符为_的员工
select last_name from employees where last_name like '_$_%' escape '$'
让_充当成一个普通的字符,要使用转译,用escape可以定义转译。
between and
- between and 包含临界值
- between a and b ,a与b不能调换顺序
eg:查询员工编号在100至120之间。
select * from employees where employ_id between 100 and 200;
in
where 字段名 in(‘a’,‘b’‘c’)
in列表的值类型必须一致或兼容
is null | is not null
where 字段名 is 【not】 null;
安全等于 <=> 可判断null值
补充函数
ifnull 函数:判断某字段的值是否为null,如果是返回原值,不是返回指定值
select ifnull(字段名,0)
isnull函数:判断某字段或表达式的值是否为null,是1 否0.