sql 查询语句
2018-08-08 14:09 misswjr 阅读(263) 评论(0) 编辑 收藏 举报1.
select * from 表名
select distinct 查询列表 from 表名 //去重查询
查表结构: DESC departments;
去重查询:select distinct location_id from departments;
字符拼接查询: select concat (字符1,字符2,字符3...);
ifnull函数:select ifnull(commission_pct,0) from employees; 判断某字段或表达式是否为null, 如果是null,返回指定的值,否则返回原本的值。
isnull:select isnull(commission_pct),commission_pct from employees; 判断某字段或表达式是否为null,如果是,则返回1,否则返回0.
2. 条件查询
select 查询列表 from 表名 where 筛选条件;
select * from employees where salary>12000;
select last_name, department_id from emplyees where department_id!=90;
select last_name,salary,commission_pct from emplyees where salary>=10000 and salary<=20000;
select last_name,salary,commission_pct from emplyees where salary between 10000 and 20000;
select last_name,salary,commission_pct from emplyees where commission_pct is null;
select last_name,salary,commission_pct from emplyees where commission_pct <=>null;
select last_name,salary,commission_pct from emplyees where commission_pct is not null;
select * from employees where last_name like '%a%';
select * from employees where last_name like '___e_a%';
select * from employees where last_name like '_\_%'; 第2个字符为_时,需要作用转义符\。
select * from employees where last_name like '_$_%' escape '$'; 指定$为转义符。
select last_name,job_id from employees where job_id='IT_PROT' or job_id='AD_VP' or job_id='AD_PRES';
select last_name,job_id from employees where job_id in ('IT_PROT','AD_VP','AD_PRES');
按条件表达式筛选:
条件运算符:> < = != <> >= <=
按逻辑表达式筛选:
作用:用于连接条件表达式
&&和and:两个条件都为true,结果为true, 反之为false
||和or:
逻辑运算符: && || ! and or not
模糊查询:
like(和通配符搭配使用 % 0个或任意多个字符,_任意单个字符) between and in(列表中的值需要一致或兼容) is null(=或<>不能用于判断null值, 仅用于判断null值,可读性高) is not null
安全等于 <=> 不仅用于判断null值,还可以判断普通数据,可读性不高。
mysql 服务的启动,停止
方式一:命令行:
net start 服务名
net stop 服务名
方式二:计算机--右击--管理--服务
Mysql服务的登录和退出
登录:mysql [-h 主机名 -P 端口号] -u 用户名 -p密码 h和主机名间可有可没有空格,P和端口号间可有可无空格,u和用户名间可有可无空格,p和密码间不需要空格
退出:exit或ctrl+C