DQL 数据查询语句
查询结构:
select 目标表的列名或列表达式集合 from基本表或(和)视图集合 【where 条件表达式】 【group by 列名集合】 【having 组条件表达式】 【order by 列名【集合】】
注意 Where,GroupBy,Having,OrderBy 顺序
1.投影操作
投影操作是查询语句里必须有的,关键字是select.
select 列1 from 表名
select 列1,列2,列3 from 表名
select * from 表名 -- 全部查询
怎么寻找到的?
先去找表,执行form后面,然后找到该表的列名
2.表前缀.列名.别名查询
select 表名.列名 from 表名
别名可以直接写中文,可以不用双引号或者单引号
第二种
别名就是为了在输出看结果的时候,改变列名,便于观察,不会改名表的列名。
select 表名.列名 as 别名 from 表名
select t_student_table.t_name from t_student_table
select t.t_name as name from t_student_table as t
这里 t.t_name 是用的表名的别名t ,在跟上列名t_name
还可以空格+别名 ,这样可以省略as
select t.t_name name from t_student_table t
select 别名.列名 as 别名 from 表名 as 别名,使用表别名的时候,原表的表名就不能再使用
select t_student_table.t_name from t_student_table as t -- 这里的表名暂时变成t了, t_student_table.列名就找不到了。 -- 注意执行顺序是先执行from,在执行select
3.计算列
在数据库管理中,我们可以
select t.t_class*10 from t_student_table as t
我们可以在返回t_class列的数据是可以计算,不影响原来的表中的值。
select concat(t.t_class*10,"班") class from t_student_table as t
concat关键字是字符串拼接函数,得到的数据是乘10后的多少班,比如10班,20班
4.去重 (distinct)
select distinct f_city from t_employee; -- 查找员工都来自什么城市 -- 去掉重复的数据,查出不同的城市
5.条件查询
where子句应用搜索条件在select查询获得的数据之后。但是条件值都是布尔值。
类别 | 运算符 | 说明 |
---|---|---|
比较运算符 | =,<,>,<=,>=,<> | 比较两个表达式 |
逻辑运算符 |