- 一、导入数据库
- 二、常用命令
- 三、函数
-
- 1、数学函数
-
- 1.1、abs(x) 返回x 的 绝对值
- 1.2、rand() 返回0到1的随机数
- 1.3、mod(x,y) 返回x除以y以后的余数
- 1.4、power(x,y) x的y次方
- 1.5、round(x)返回离x最近的整数
- 1.6、round(x,y)保留x的y位小数四舍五入后的值
- 1.7、sqrt(x)返回x的平方根
- 1.8、truncate(x,y)返回数字 x 截断为 y 位小数的值
- 1.9、ceil(x)返回大于或等于 x 的最小整数
- 1.10、floor(x)返回小于或等于 x 的最大整数
- 1.11、greatest(x1,x2.....)返回返回集合中最大的值
- 1.12、least(x1,x2..........)返回返回集合中最小的值
- 2、聚合函数
- 3、字符串函数
- 四、汇总分组group by
- 五、聚会函数条件语句having
- 六、连接查询
- 七、别名 as
- 八、子查询
- 九、判断exists
- 十、视图CREATE VIEW
- 十一、联集 UNION
- 十二、条件语句case
一、导入数据库
先把数据表导入系统

然后在数据库里
(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;

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

被折叠的 条评论
为什么被折叠?



