SQL高级语言

本文详细介绍了SQL的各种高级查询技巧,包括导入数据库、常用命令如SELECT、DISTINCT、WHERE等,以及数学函数、聚合函数、字符串函数的使用。此外,还讲解了GROUP BY、HAVING、JOIN、别名AS、子查询、EXISTS、视图CREATE VIEW、UNION和CASE条件语句的应用,是提升SQL查询能力的好资源。
摘要由CSDN通过智能技术生成

一、导入数据库

先把数据表导入系统
在这里插入图片描述
然后在数据库里

(root@localhost) [(none)]> source test.sql
Query OK, 0 rows affected (0.00 sec)

就可以看到导入的数据库了
在这里插入图片描述

二、常用命令

1、select

显示表格中的一个或者多个字段中所有的信息
语法:
select 字段名[,字段名2] from 表名;
列出表的所有内容
在这里插入图片描述
列出age列所有数值
在这里插入图片描述

2、 distinct

查询不重复记录
语法:
select distinct 字段 from 表名﹔

在这里插入图片描述
列出年龄所有数值

(root@localhost) [hellodb]> select distinct age from students;

在这里插入图片描述
列出性别所有数值
在这里插入图片描述

3、 where

有条件的查询
语法:select ‘字段’ from 表名 where 条件
找出满足条件为年龄在20岁以下的

(root@localhost) [hellodb]> select *  from students where age <20;

在这里插入图片描述

4、and;or

and 且 or 或
语法:
select 字段名 from 表名 where 条件1 (and|or) 条件2 (and|or)条件3;
找出年龄在20到30之间的

(root@localhost) [hellodb]> select *  from students where age >20 and age<30;

在这里插入图片描述
查找年龄大于20或者学号为3的

(root@localhost) [hellodb]> select *  from students where age >20 or  stuid =3;

在这里插入图片描述

5、in

显示已知值的资料
语法:
select 字段名 from 表名 where 字段 in (‘值1’,‘值2’…);
对一个已知值查找

(root@localhost) [hellodb]> select *  from students where age in (22);

在这里插入图片描述
可以对多个已知值查找

(root@localhost) [hellodb]> select *  from students where age in (22,25,23,100);

在这里插入图片描述

6、between

语法:
select 字段名 from 表名 where 字段 between ‘值1’ and ‘值2’;
包括 and两边的值
查找学号2-到5 的信息

(root@localhost) [hellodb]> select * from students where stuid between 2 and 5;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值