学习之SQL语句之DQL(数据库查询语言)

DQL英文全称是Data Query Language(数据查询语言),数据查询语言,用来查询数据库中表的记录

查询关键字:SELECT

数据准备:

CREATE TABLE emp (

id INT COMMENT "编号",

workno VARCHAR ( 10 ) COMMENT "工号",

NAME VARCHAR ( 10 ) COMMENT "姓名",

gender CHAR ( 1 ) COMMENT "性别",

age TINYINT UNSIGNED COMMENT "年龄",

idcard CHAR ( 18 ) COMMENT "身份证号",

workaddress VARCHAR ( 50 ) COMMENT "工作地址",

entrydate DATE COMMENT "入职时间"

) COMMENT "员工表";

INSERT INTO emp ( id, workno, NAME, gender, age, idcard, workaddress, entrydate )

VALUES

( 1, "1", "柳岩", "女", 20, "123456789012345678", "北京", "2000-01-01" ),

( 2, "2", "张无忌", "男", 18, "123456789012345670", "北京", "2005-09-01" ),

( 3, "3", "韦一笑", "男", 38, "123456789712345670", "上海", "2005-08-01" ),

( 4, "4", "赵敏", "女", 18, "123456757123845670", "北京", "2009-12-01" ),

( 5, "5", "小昭", "女", 16, "123456769012345678", "上海", "2007-07-01" ),

( 6, "6", "杨道", "男", 28, "12345678931234567X", "北京", "2006-01-01" ),

( 7, "7", "范瑶", "男", 40, "12345678921234567日", "北京", "2005-05-01" ),

( 8, "8", "黛绮丝", "女", 38, "123456157123645670", "天津", "2015-05-01"),

( 9, "9", "范凉凉", "女", 45, "123156789012345678", "北京", "2010-04-01" ),

( 10, "1", "陈友谅", "男", 53, "123456789012345670", "上海", "2011-01-01" ),

( 11, "11", "张士诚", "男", 55, "123567897123465670", "江苏", "2015-05-01" ),

( 12, "12", "常遇春", "男", 32, "123446757152345670", "北京", "2004-02-01" ),

( 13, "13", "张三丰", "男", 88, "123656789012345678","江苏", "2020-11-01" ),

( 14, "14", "灭绝", "女", 65, "123456719012345670", "西安", "2019-05-01" ),

( 15, "15", "胡青牛", "男", 70, "12345674971234567X", "西安", "2018-04-01" ),

( 16, "16","周芷若", "女", 18, NULL, "北京", "2012-06-01" );

DQL -基本查询

1、查询多个字段

SELECT 字段1,字段2... FROM 表名;

SELECT * FROM 表名;

2、设置别名

SELECT 字段1 [AS 别名],字段2[AS 别名]... FROM 表名;

3、去除重复记录

SELECT DISTINCT 字段列表 FROM 表名;

案例:

-- 1.查询指定字段 name,workno,age 返回

select name, workno,age from emp;

-- 2.查询所有字段返回

select id, workno, name, gender, age, idcard,workaddress, entrydate from emp;

select * from emp;
-- 3.查询所有员工的工作地址,起别名

select workaddress as '工作地址' from emp;

select workaddress "工作地址" from emp;

-- 4.查询公司员工的上班地址(不要重复)

select distinct workaddress from emp;

DQL - 条件查询

案例:

DQL -聚合函数


DQL -分组查询


DQL - 排序查询


DQL-分页查询

-- DQL练习


相关推荐
无敌岩雀2 分钟前
MySQL中的索引
数据库·mysql
a_安徒生24 分钟前
linux安装TDengine
linux·数据库·tdengine
程序员学习随笔27 分钟前
PostgreSQL技术内幕19:逻辑备份工具pg_dump、pg_dumpall
数据库·postgresql
心怀梦想的咸鱼31 分钟前
UE5 第一人称射击项目学习(四)
学习·ue5
AI完全体34 分钟前
【AI日记】24.11.22 学习谷歌数据分析初级课程-第2/3课
学习·数据分析
尘浮生1 小时前
Java项目实战II基于微信小程序的校运会管理系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
偶尔。5351 小时前
什么是事务?事务有哪些特性?
数据库·oracle
安迁岚1 小时前
【SQL Server】华中农业大学空间数据库实验报告 实验六 视图
数据库·sql·mysql·oracle·实验报告
Mephisto.java1 小时前
【大数据学习 | Spark-Core】Spark提交及运行流程
大数据·学习·spark
xoxo-Rachel1 小时前
(超级详细!!!)解决“com.mysql.jdbc.Driver is deprecated”警告:详解与优化
java·数据库·mysql