【数据库】函数,谓词(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

相关文章

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

如何通过try-catch判断数据库唯一键字段是否重复

《如何通过try-catch判断数据库唯一键字段是否重复》在MyBatis+MySQL中,通过try-catch捕获唯一约束异常可避免重复数据查询,优点是减少数据库交互、提升并发安全,缺点是异常处理开... 目录1、原理2、怎么理解“异常走的是数据库错误路径,开销比普通逻辑分支稍高”?1. 普通逻辑分支 v

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

Python中的filter() 函数的工作原理及应用技巧

《Python中的filter()函数的工作原理及应用技巧》Python的filter()函数用于筛选序列元素,返回迭代器,适合函数式编程,相比列表推导式,内存更优,尤其适用于大数据集,结合lamb... 目录前言一、基本概念基本语法二、使用方式1. 使用 lambda 函数2. 使用普通函数3. 使用 N

MySQL中REPLACE函数与语句举例详解

《MySQL中REPLACE函数与语句举例详解》在MySQL中REPLACE函数是一个用于处理字符串的强大工具,它的主要功能是替换字符串中的某些子字符串,:本文主要介绍MySQL中REPLACE函... 目录一、REPLACE()函数语法:参数说明:功能说明:示例:二、REPLACE INTO语句语法:参数