首先下载myemployees,这是一个数据库文件,里面有四张表,百度网盘下载地址如下:
myemployees.sql:https://pan.baidu.com/s/18vBejWBJK8oG-9GjSBk4Ng
密码:6666
下载好之后,打开SQLyog8.0.15,右键root@localhost,点击“执行SQL脚本”,选中“myemployees.sql”,然后点击“执行”,执行完毕后,点击“完成”,然后点击“刷新对象浏览器(F5)”,就出来以下的画面了。
DQL语言-基础查询
#进阶1:基础查询
/*
语法:
select 查询列表 from 表名;
类似于:System.out.printin(打印东西);或printf(打印东西);或cout<<(打印东西);
特点:
1、查询列表可以是:表中的字段、常量值、表达式、函数
2、查询的结果是一个虚拟的表格
*/
USE myemployees;
#1.查询表中的单个字段
SELECT last_name FROM employees;
#2.查询表中的多个字段
SELECT last_name,salary,email FROM employees ;
#3.查询表中的所有字段
法一:
SELECT
`employee_id`,
`first_name`,
`last_name`,
`email`,
`phone_number`,
`job_id`,
`salary`,
`commission_pct`,
`manager_id`,
`hiredate`,
`department_id`
FROM
employees ;
法二:
SELECT * FROM employees;
#4.查询常量值
①查询常量
SELECT 100 ;
②查询字符
SELECT 'john' ;
#5.查询表达式
SELECT 100 % 98 ;
#6.查询函数
SELECT VERSION() ;
#7.起别名
/*
①便于理解
②如果要查询的字段有重名的情况,使用别名可以区分开来
*/
#方法一:使用as
①select 100 % 98 AS 结果 ;
②select `last_name` AS 姓,`first_name` AS 名 FROM employees ;
#方法二:使用空格
SELECT `last_name` 姓,`first_name` 名 FROM employees;
案例:查询salary,显示结果为out put
SELECT salary AS "out put" FROM employees;
#8.去重
SELECT DISTINCT `department_id` FROM employees;
#9.+号的作用
/*
java中的+号:
①运算符,两个操作数都为数值型,"+“就是运算符
②连接符,只要有一个操作数为字符串,”+"就是连接符
mysql中的+号:
仅仅只有一个功能:运算符
select 100+90; 两个操作数都为数值型,则做加法运算
select ‘123’+90;只要其中一方为字符型,视图将字符数值转换成数值型
如果转换成功,则继续做加法运算
select ‘john’+50; 如果转换失败,则将字符型数值转换成0
select null+10; 只要其中一方为null,则结果肯定为null
select null+null;
*/
#案例:查询员工名和姓连接成一个字段,并显示为 姓名
①select CONCAT('a','b','c') AS 结果;
②select
CONCAT(`last_name`, `first_name`) AS 姓名
FROM
employees ;
练习:
#1. 下面的语句是否可以执行成功
SELECT last_name,job_id,salary AS sal
FROM employees;
答案:可以执行成功
#2.下面的语句是否可以执行成功
SELECT * FROM employees;
答案:可以执行成功
#3.找出下面语句中的错误
SELECT employee_id,last_name,
salary * 12 AS "ANNUAL SALARY"
FROM employees;
改正
SELECT employee_id,last_name,
salary * 12 AS "ANNUAL SALARY"
FROM employees;
#4显示表departments的结构,并查询其中的全部数据
DESC departments;
SELECT * FROM `departments`;
#5.显示出表employees中的全部job_id(不能重复)
SELECT DISTINCT job_id FROM employees;
#6.显示出表employees的全部列,各个列之间用逗号连接,列头显示成OUT_PUT
INFULL作用:如果commission_pct为NULL,则奖金率为0.00
SELECT
IFNULL(`commission_pct`,0) AS 奖金率,
`commission_pct`
FROM
employees;
#----------------------------------------------------
第6题答案:
SELECT
CONCAT(`first_name`,',',`last_name`,',',`job_id`,',',IFNULL(`commission_pct`,0))AS out_put
FROM
employees;