二.1.DQL语言-基础查询

首先下载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` ASFROM 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;
mysql示例数据库 employee,这个大家也可以到github官网下载。 https://github.com/datacharmer/test_db test_db A sample database with an integrated test suite, used to test your applications and database servers This repository was migrated from Launchpad. See usage in the MySQL docs Where it comes from The original data was created by Fusheng Wang and Carlo Zaniolo at Siemens Corporate Research. The data is in XML format. http://timecenter.cs.aau.dk/software.htm Giuseppe Maxia made the relational schema and Patrick Crews exported the data in relational format. The database contains about 300,000 employee records with 2.8 million salary entries. The export data is 167 MB, which is not huge, but heavy enough to be non-trivial for testing. The data was generated, and as such there are inconsistencies and subtle problems. Rather than removing them, we decided to leave the contents untouched, and use these issues as data cleaning exercises. Prerequisites You need a MySQL database server (5.0+) and run the commands below through a user that has the following privileges: SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW Installation: Download the repository Change directory to the repository Then run mysql < employees.sql If you want to install with two large partitioned tables, run mysql < employees_partitioned.sql Testing the installation After installing, you can run one of the following mysql -t < test_employees_md5.sql # OR mysql -t < test_employees_sha.sql For example: mysql -t < test_employees_md5.sql +----------------------+ | INFO | +----------------------+ | TESTING INSTALLATION | +----------------------+ +--------------+------------------+----------------------------------+ | table_name | expected_records | expected_crc | +--------------+------------------+----------------------------------+ | employees | 300024 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值