如何在 SQL (Postgres) 中查找子字符串 [英] How to find substrings in SQL (Postgres)

查看:151
本文介绍了如何在 SQL (Postgres) 中查找子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的数据库表中有一个名为code"的列,其中包含字符串数据类型.

Let's say I have a column called 'code' in my database table containing string data types.

我想要一个查询,该查询将返回具有代码"值的所有行,该值是给定搜索字符串的子字符串,附加条件是该子字符串出现在搜索字符串的末尾.

I want a query that will return all rows with a 'code' value that is a substring of a given search string, with the added condition that the substring appears at the end of the search string.

例如,如果搜索字符串为12345",则查询应返回代码"值为以下的所有行:
12345
2345
345
45
5

For example, if the search string is '12345', the query should return all rows with a 'code' value of:
12345
2345
345
45
5

推荐答案

只需使用 like 运算符:

Simply use like operator:

SELECT * from yourtable
where '12345' like '%' || Code 

这篇关于如何在 SQL (Postgres) 中查找子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆