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

相关文章

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Java中Switch Case多个条件处理方法举例

《Java中SwitchCase多个条件处理方法举例》Java中switch语句用于根据变量值执行不同代码块,适用于多个条件的处理,:本文主要介绍Java中SwitchCase多个条件处理的相... 目录前言基本语法处理多个条件示例1:合并相同代码的多个case示例2:通过字符串合并多个case进阶用法使用

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

数据库面试必备之MySQL中的乐观锁与悲观锁

《数据库面试必备之MySQL中的乐观锁与悲观锁》:本文主要介绍数据库面试必备之MySQL中乐观锁与悲观锁的相关资料,乐观锁适用于读多写少的场景,通过版本号检查避免冲突,而悲观锁适用于写多读少且对数... 目录一、引言二、乐观锁(一)原理(二)应用场景(三)示例代码三、悲观锁(一)原理(二)应用场景(三)示例

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序