【数据库】函数,谓词(LIKE,BETWEEN,IN),CASE

2024-03-24 21:30

本文主要是介绍【数据库】函数,谓词(LIKE,BETWEEN,IN),CASE,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

6.1函数
算术函数
6.1 创建表格:

postgres=# CREATE TABLE SampleMath
postgres-# (m NUMERIC (10,3),
postgres(# n INTEGER,
postgres(# p INTEGER);
CREATE TABLE
postgres=# BEGIN TRANSACTION;
BEGIN
postgres=# INSERT INTO SampleMath(m, n, p) VALUES (500,  0,    NULL);
INSERT 0 1
postgres=# INSERT INTO SampleMath(m, n, p) VALUES (-180, 0,    NULL);
INSERT 0 1
postgres=# INSERT INTO SampleMath(m, n, p) VALUES (NULL, NULL, NULL);
INSERT 0 1
postgres=# INSERT INTO SampleMath(m, n, p) VALUES (NULL, 7,    3);
INSERT 0 1
postgres=# INSERT INTO SampleMath(m, n, p) VALUES (NULL, 5,    2);
INSERT 0 1
postgres=# INSERT INTO SampleMath(m, n, p) VALUES (NULL, 4,    NULL);
INSERT 0 1
postgres=# INSERT INTO SampleMath(m, n, p) VALUES (8,    NULL, 3);
INSERT 0 1
postgres=# INSERT INTO SampleMath(m, n, p) VALUES (2.27, 1,    NULL);
INSERT 0 1
postgres=# INSERT INTO SampleMath(m, n, p) VALUES (5.555,2,    NULL);
INSERT 0 1
postgres=# INSERT INTO SampleMath(m, n, p) VALUES (NULL, 1,    NULL);
INSERT 0 1
postgres=# INSERT INTO SampleMath(m, n, p) VALUES (8.76, NULL, NULL);
INSERT 0 1
postgres=# COMMIT;
COMMIT
postgres=# SELECT * FROM SampleMath;m     | n | p
----------+---+---500.000 | 0 |-180.000 | 0 ||   || 7 | 3| 5 | 2| 4 |8.000 |   | 32.270 | 1 |5.555 | 2 || 1 |8.760 |   |
(11 行记录)

6.2 求绝对值

postgres=# SELECT m,
postgres-# ABS(m) AS abs_col
postgres-# FROM SampleMath;m     | abs_col
----------+---------500.000 | 500.000-180.000 | 180.000||||8.000 |   8.0002.270 |   2.2705.555 |   5.555|8.760 |   8.760
(11 行记录)

6.3 求余数MOD

postgres=# SELECT n,p,
postgres-# MOD(n,p) AS mod_col
postgres-# FROM SampleMath;n | p | mod_col
---+---+---------0 |   |0 |   ||   |7 | 3 |       15 | 2 |       14 |   || 3 |1 |   |2 |   |1 |   ||   |
(11 行记录)

6.4 ROUND四舍五入,m时待处理数据,n是四舍五入之后的位数

postgres=# SELECT m,n,
postgres-# ROUND(m,n) AS round_col
postgres-# FROM SampleMath;m     | n | round_col
----------+---+-----------500.000 | 0 |       500-180.000 | 0 |      -180|   || 7 || 5 || 4 |8.000 |   |2.270 | 1 |       2.35.555 | 2 |      5.56| 1 |8.760 |   |
(11 行记录)

6.5 创建字符串表

postgres=# CREATE TABLE SampleStr
postgres-# (str1  VARCHAR(40),
postgres(#  str2  VARCHAR(40),
postgres(#  str3  VARCHAR(40));
CREATE TABLE
postgres=# BEGIN TRANSACTION;
BEGIN
postgres=# INSERT INTO SampleStr (str1, str2, str3) VALUES ('opx',        'rt',NULL);
INSERT 0 1
postgres=# INSERT INTO SampleStr (str1, str2, str3) VALUES ('abc','def',NULL);
INSERT 0 1
postgres=# INSERT INTO SampleStr (str1, str2, str3) VALUES ('山田',' 太郎'  ,'是我');
INSERT 0 1
postgres=# INSERT INTO SampleStr (str1, str2, str3) VALUES ('aaa',NULL    ,NULL);
INSERT 0 1
postgres=# INSERT INTO SampleStr (str1, str2, str3) VALUES (NULL,'xyz',        NULL);
INSERT 0 1
postgres=# INSERT INTO SampleStr (str1, str2, str3) VALUES ('@!#$%',NULL,NULL);
INSERT 0 1
postgres=# INSERT INTO SampleStr (str1, str2, str3) VALUES ('ABC',NULL,NULL);
INSERT 0 1
postgres=# INSERT INTO SampleStr (str1, str2, str3) VALUES ('aBC',NULL,NULL);
INSERT 0 1
postgres=# INSERT INTO SampleStr (str1, str2, str3) VALUES ('abc太郎','abc','ABC');
INSERT 0 1
postgres=# INSERT INTO SampleStr (str1, str2, str3) VALUES ('abcdefabc',   'abc','ABC');
INSERT 0 1
postgres=# INSERT INTO SampleStr (str1, str2, str3) VALUES ('micmic',      'i',        'I');;
INSERT 0 1
postgres=# COMMIT;
COMMIT
postgres=# SELECT * FROM SampleStr;str1    | str2 | str3
-----------+------+------opx       | rt   |abc       | def  |山田      | 太郎 | 是我aaa       |      || xyz  |@!#$%     |      |ABC       |      |aBC       |      |abc太郎   | abc  | ABCabcdefabc | abc  | ABCmicmic    | i    | I
(11 行记录)

6.6 字符串拼接||

postgres=# SELECT str1,str2,
postgres-# str1||str2 AS str_concat
postgres-# FROM SampleStr;str1    | str2 |  str_concat
-----------+------+--------------opx       | rt   | opxrtabc       | def  | abcdef山田      | 太郎 | 山田太郎aaa       |      || xyz  |@!#$%     |      |ABC       |      |aBC       |      |abc太郎   | abc  | abc太郎abcabcdefabc | abc  | abcdefabcabcmicmic    | i    | micmici
(11 行记录)

6.7 拼接三个字符串

postgres=# SELECT str1,str2,str3,
postgres-# str1||str2||str3 AS str_concat
postgres-# FROM SampleStr
postgres-# WHERE str1='山田';str1 | str2 | str3 |  str_concat
------+------+------+--------------山田 | 太郎 | 是我 | 山田太郎是我
(1 行记录)

6.8 计算字符串长度LENGTH

postgres=# SELECT str1,
postgres-# LENGTH(str1) AS length_str
postgres-# FROM SampleStr;str1    | length_str
-----------+------------opx       |          3abc       |          3山田      |          2aaa       |          3|@!#$%     |          5ABC       |          3aBC       |          3abc太郎   |          5abcdefabc |          9micmic    |          6
(11 行记录)

6.9 大小写转换 LOWER UPPER

postgres=# SELECT str1,
postgres-# LOWER(str1) AS low_str
postgres-# FROM SampleStr;str1    |  low_str
-----------+-----------opx       | opxabc       | abc山田      | 山田aaa       | aaa|@!#$%     | @!#$%ABC       | abcaBC       | abcabc太郎   | abc太郎abcdefabc | abcdefabcmicmic    | micmic
(11 行记录)

6.10 替换字符串REPLACE(对象字符串,替换前,替换后)

postgres=# SELECT str1,str2,str3,
postgres-# REPLACE(str1,str2,str3) AS replace_str
postgres-# FROM SampleStr;str1    | str2 | str3 | replace_str
-----------+------+------+-------------opx       | rt   |      |abc       | def  |      |山田      | 太郎 | 是我 | 山田aaa       |      |      || xyz  |      |@!#$%     |      |      |ABC       |      |      |aBC       |      |      |abc太郎   | abc  | ABC  | ABC太郎abcdefabc | abc  | ABC  | ABCdefABCmicmic    | i    | I    | mIcmIc
(11 行记录)

6.11 字符串的截取SUBSTRING(对象字符串 FROM 第几位 FRO 截取长度)

postgres=# SELECT str1,
postgres-# SUBSTRING(str1 FROM 3 FOR 2) AS subs_str
postgres-# FROM SampleStr;str1    | subs_str
-----------+----------opx       | xabc       | c山田      |aaa       | a|@!#$%     | #$ABC       | CaBC       | Cabc太郎   | c太abcdefabc | cdmicmic    | cm
(11 行记录)

日期函数:
6.13 获得当前日期CURRENT_DATE

postgres=# SELECT CURRENT_DATE
postgres-# ;date
------------2021-04-14
(1 行记录)

6.14获取当前时间 CURRENT_TIME

postgres=# SELECT CURRENT_TIME;timetz
--------------------11:18:22.191069+08
(1 行记录)

6.15 获取当前的日期和时间 CURRENT_TIMESTAMP

postgres=# SELECT CURRENT_TIMESTAMP;now
-------------------------------2021-04-14 11:19:16.623151+08
(1 行记录)

6.16 截取日期元素

postgres=# SELECT CURRENT_TIMESTAMP,
postgres-# EXTRACT(YEAR FROM CURRENT_TIMESTAMP) AS year,
postgres-# EXTRACT(MONTH FROM CURRENT_TIMESTAMP) AS month;now              | year | month
-------------------------------+------+-------2021-04-14 11:25:10.543004+08 | 2021 |     4
(1 行记录)

转换函数
6.17 数据类型转换CAST
格式:CAST(待转换对象 AS 转换后的数据类型)

postgres=# SELECT CAST('0001' AS INTEGER) AS int_col;int_col
---------1
(1 行记录)

6.18 将字符串转换为日期类型

postgres=# SELECT CAST('2009-12-11' AS DATE) AS date_col;date_col
------------2009-12-11
(1 行记录)

6.19 将NULL值转换为其他值COALESCE
返回COALESCE括号内第一个不为0的索引。

postgres=# SELECT COALESCE(NULL,1) AS col_1,
postgres-# COALESCE(2) AS col_2,
postgres-# COALESCE(NULL,NULL,NULL,NULL,4) AS col_3;col_1 | col_2 | col_3
-------+-------+-------1 |     2 |     4
(1 行记录)

6.20 COALESCE作用于某一列

postgres=# SELECT COALESCE(str2,'大哥')
postgres-# FROM SampleStr;coalesce
----------rtdef太郎大哥xyz大哥大哥大哥abcabci
(11 行记录)

6.2 谓词
6.21创建SampleLike表

postgres=# CREATE TABLE SampleLike
postgres-# (strcol VARCHAR(6) NOT NULL,
postgres(# PRIMARY KEY (strcol));
CREATE TABLE
postgres=# BEGIN TRASACTION;
错误:  语法错误 在 "TRASACTION" 或附近的
第1BEGIN TRASACTION;^
postgres=# BEGIN TRANSACTION;
BEGIN
postgres=# INSERT INTO SampleLike (strcol) VALUES ('abcddd');
INSERT 0 1
postgres=# INSERT INTO SampleLike (strcol) VALUES ('dddabc');
INSERT 0 1
postgres=# INSERT INTO SampleLike (strcol) VALUES ('abdddc');
INSERT 0 1
postgres=# INSERT INTO SampleLike (strcol) VALUES ('abcdd');
INSERT 0 1
postgres=# INSERT INTO SampleLike (strcol) VALUES ('ddabc');
INSERT 0 1
postgres=# INSERT INTO SampleLike (strcol) VALUES ('abddc');
INSERT 0 1
postgres=# COMMIT;
COMMITpostgres=# SELECT * FROM SampleLike;strcol
--------abcddddddabcabdddcabcddddabcabddc
(6 行记录)

6.22 用LIKE语句做前方一致查询

postgres=# SELECT *
postgres-# FROM SampleLike
postgres-# WHERE strcol LIKE 'ddd%';strcol
--------dddabc
(1 行记录)

注:%表示任意字符串
6.22 中间一致查询

postgres=# SELECT * FROM SampleLike
postgres-# WHERE strcol LIKE '%ddd%';strcol
--------abcddddddabcabdddc
(3 行记录)

注:中间的查询结果包含前后
6.23 后方一致查询

postgres=# SELECT * FROM SampleLike
postgres-# WHERE strcol LIKE '%ddd';strcol
--------abcddd
(1 行记录)

6.26 用下划线表示任意一个字符

postgres=# SELECT * FROM SampleLike
postgres-# WHERE strcol LIKE '___dd';strcol
--------abcdd
(1 行记录)

6.27 选取销售单价在100-5000之间的商品BETWEEN

postgres=# SELECT product_name,sale_price
postgres-# FROM Product
postgres-# WHERE sale_price BETWEEN 100 AND 5000;product_name | sale_price
--------------+------------T恤          |       1000打孔器       |        500运动T恤      |       4000菜刀         |       3000叉子         |        500擦菜板       |        880圆珠笔       |        100
(7 行记录)

注:默认是闭区间,想要开区间,在数据前加<或>
6.29 选取进价为NULL的数据 ISNULL

postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE purchase_price ISNULL;product_name | purchase_price
--------------+----------------叉子         |圆珠笔       |
(2 行记录)

6.30 选取不为NULL的数据 IS NOT NULL

postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE purchase_price IS NOT NULL;product_name | purchase_price
--------------+----------------T恤          |            500打孔器       |            320运动T恤      |           2800菜刀         |           2800高压锅       |           5000擦菜板       |            790
(6 行记录)

注:注意写法,中间有空格
6.32 用IN 指定多个具体的数据

postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE purchase_price IN (500,320,790);product_name | purchase_price
--------------+----------------T恤          |            500打孔器       |            320擦菜板       |            790
(3 行记录)

6.33 返回不在IN集合的数据

postgres=# SELECT product_name,purchase_price
postgres-# FROM Product
postgres-# WHERE purchase_price NOT IN (500,320,790);product_name | purchase_price
--------------+----------------运动T恤      |           2800菜刀         |           2800高压锅       |           5000
(3 行记录)

注:NULL 数据不参与
6.34 创建ShopProduct表

postgres=# SELECT * FROM ShopProduct;shop_id | shop_name | product_id | quantity
---------+-----------+------------+----------000A    | 东京      | 0001       |       30000A    | 东京      | 0002       |       50000A    | 东京      | 0003       |       15000B    | 名古屋    | 0002       |       30000B    | 名古屋    | 0003       |      120000B    | 名古屋    | 0004       |       20000B    | 名古屋    | 0006       |       10000B    | 名古屋    | 0007       |       40000C    | 大阪      | 0003       |       20000C    | 大阪      | 0004       |       50000C    | 大阪      | 0006       |       90000C    | 大阪      | 0007       |       70000D    | 福冈      | 0001       |      100
(13 行记录)

6.36 使用子查询作为IN的参数

postgres=# SELECT product_name,sale_price
postgres-# FROM Product
postgres-# WHERE product_id IN (SELECT product_id FROM ShopProduct
postgres(# WHERE shop_name='大阪');product_name | sale_price
--------------+------------叉子         |        500擦菜板       |        880运动T恤      |       4000菜刀         |       3000
(4 行记录)

注:EXIST了解,多数情况下可以用IN代替

6.41 通过CASE语句给种类加ABC

postgres=# SELECT product_name,
postgres-# CASE WHEN product_type='衣服'
postgres-# THEN 'A:'|| product_type
postgres-# WHEN product_type='办公用品'
postgres-# THEN 'B:'|| product_type
postgres-# WHEN product_type='厨房用具'
postgres-# THEN 'C:'|| product_type
postgres-# ELSE NULL
postgres-# END AS nayilie
postgres-# FROM Product;product_name |  nayilie
--------------+------------T恤          | A:衣服打孔器       | B:办公用品运动T恤      | A:衣服菜刀         | C:厨房用具高压锅       | C:厨房用具叉子         | C:厨房用具擦菜板       | C:厨房用具圆珠笔       | B:办公用品
(8 行记录)

6.43 CASE表达式进行列转换

postgres=# SELECT SUM(CASE WHEN product_type='衣服'
postgres(# THEN sale_price ELSE 0 END) AS clothes,
postgres-# SUM(CASE WHEN product_type='厨房用具'
postgres(# THEN sale_price ELSE 0 END) AS kitchen,
postgres-# SUM(CASE WHEN product_type='办公用品'
postgres(# THEN sale_price ELSE 0 END) AS office
postgres-# FROM Product;clothes | kitchen | office
---------+---------+--------5000 |   11180 |    600
(1 行记录)

练习:
6.1 计算执行结果
在这里插入图片描述

 product_name | purchase_price
--------------+----------------
(0 行记录)

注:IN 无法选取NULL或者NOT NULL数据。
6.2 CASE问题
在这里插入图片描述

postgres=# SELECT SUM(CASE WHEN sale_price <= 1000 THEN 1 ELSE 0 END)               AS low_price,
postgres-#        SUM(CASE WHEN sale_price BETWEEN 1001 AND 3000 THEN 1 ELSE 0 END) AS mid_price,
postgres-#        SUM(CASE WHEN sale_price >= 3001 THEN 1 ELSE 0 END)               AS high_price
postgres-#  FROM Product;low_price | mid_price | high_price
-----------+-----------+------------5 |         1 |          2
(1 行记录)

这篇关于【数据库】函数,谓词(LIKE,BETWEEN,IN),CASE的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/842840

相关文章

PostgreSQ数据库实现在Windows上异地自动备份指南的详细教程

《PostgreSQ数据库实现在Windows上异地自动备份指南的详细教程》这篇文章主要为大家详细介绍了如何在Windows系统上实现PostgreSQL数据库的异地自动备份,文中的示例代码讲解详细,... 目录前期准备实现步骤步骤一:创建备份脚本步骤二:配置免密登录(可选但推荐)步骤三:设置任务计划程序步

Mybatis对MySQL if 函数的不支持问题解读

《Mybatis对MySQLif函数的不支持问题解读》接手项目后,为了实现多租户功能,引入了Mybatis-plus,发现之前运行正常的SQL语句报错,原因是Mybatis不支持MySQL的if函... 目录MyBATis对mysql if 函数的不支持问题描述经过查询网上搜索资料找到原因解决方案总结Myb

Python容器转换与共有函数举例详解

《Python容器转换与共有函数举例详解》Python容器是Python编程语言中非常基础且重要的概念,它们提供了数据的存储和组织方式,下面:本文主要介绍Python容器转换与共有函数的相关资料,... 目录python容器转换与共有函数详解一、容器类型概览二、容器类型转换1. 基本容器转换2. 高级转换示

JavaWeb项目创建、部署、连接数据库保姆级教程(tomcat)

《JavaWeb项目创建、部署、连接数据库保姆级教程(tomcat)》:本文主要介绍如何在IntelliJIDEA2020.1中创建和部署一个JavaWeb项目,包括创建项目、配置Tomcat服务... 目录简介:一、创建项目二、tomcat部署1、将tomcat解压在一个自己找得到路径2、在idea中添加

MySQL MHA集群详解(数据库高可用)

《MySQLMHA集群详解(数据库高可用)》MHA(MasterHighAvailability)是开源MySQL高可用管理工具,用于自动故障检测与转移,支持异步或半同步复制的MySQL主从架构,本... 目录mysql 高可用方案:MHA 详解与实战1. MHA 简介2. MHA 的组件组成(1)MHA

pandas使用apply函数给表格同时添加多列

《pandas使用apply函数给表格同时添加多列》本文介绍了利用Pandas的apply函数在DataFrame中同时添加多列,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录一、Pandas使用apply函数给表格同时添加多列二、应用示例一、Pandas使用apply函

MySQL 数据库进阶之SQL 数据操作与子查询操作大全

《MySQL数据库进阶之SQL数据操作与子查询操作大全》本文详细介绍了SQL中的子查询、数据添加(INSERT)、数据修改(UPDATE)和数据删除(DELETE、TRUNCATE、DROP)操作... 目录一、子查询:嵌套在查询中的查询1.1 子查询的基本语法1.2 子查询的实战示例二、数据添加:INSE

Python中Namespace()函数详解

《Python中Namespace()函数详解》Namespace是argparse模块提供的一个类,用于创建命名空间对象,它允许通过点操作符访问数据,比字典更易读,在深度学习项目中常用于加载配置、命... 目录1. 为什么使用 Namespace?2. Namespace 的本质是什么?3. Namesp

通过DBeaver连接GaussDB数据库的实战案例

《通过DBeaver连接GaussDB数据库的实战案例》DBeaver是一个通用的数据库客户端,可以通过配置不同驱动连接各种不同的数据库,:本文主要介绍通过DBeaver连接GaussDB数据库的... 目录​一、前置条件​二、连接步骤​三、常见问题与解决方案​1. 驱动未找到​2. 连接超时​3. 权限不

MySQL中如何求平均值常见实例(AVG函数详解)

《MySQL中如何求平均值常见实例(AVG函数详解)》MySQLavg()是一个聚合函数,用于返回各种记录中表达式的平均值,:本文主要介绍MySQL中用AVG函数如何求平均值的相关资料,文中通过代... 目录前言一、基本语法二、示例讲解1. 计算全表平均分2. 计算某门课程的平均分(例如:Math)三、结合