【问题标题】:emulating MySQL's substring_index() in PGSQL在 PGSQL 中模拟 MySQL 的 substring_index()
【发布时间】:2013-10-07 17:01:52
【问题描述】:

我想找到一种优雅的方式来模拟 Postgres 中 MySQL 的 subtring_index() 函数的行为。

在 MySQL 中,这很简单:

mysql> create temporary table test1(test varchar(200));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test1 values('apples||oranges'),('apples||grapes');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from test1;
+-----------------+
| test            |
+-----------------+
| apples||oranges |
| apples||grapes  |
+-----------------+
2 rows in set (0.00 sec)

mysql> select substring_index(test, '||', 1) as field1, substring_index(test, '||', -1) as field2 from test1;
+--------+---------+
| field1 | field2  |
+--------+---------+
| apples | oranges |
| apples | grapes  |
+--------+---------+
2 rows in set (0.00 sec)

但我目前在 PGSQL 中的工作非常难看:

hoth=# create temporary table test1(test text);
CREATE TABLE

hoth=# insert into test1 values('apples||oranges'),('apples||grapes');
INSERT 0 2

hoth=# select * from test1;
      test       
-----------------
 apples||oranges
 apples||grapes
(2 rows)

hoth=# select substring(test, 0, position('||' in test)) as field1,  substring(test, position('||' in test) + 2, char_length(test)) as field2  from test1;
 field1 | field2  
--------+---------
 apples | oranges
 apples | grapes
(2 rows)

也许有一个使用正则表达式的更优雅的解决方案,或者甚至可以通过将字符串拆分为变量中的数组,如果字符串是从子查询或其他东西派生的,这可能会减少开销,我欢迎任何建议。

【问题讨论】:

  • 我想开箱即用的解决方案是以更适合您想要执行的查询的方式存储您的数据(例如,通过对其进行规范化或使用数组类型)。我意识到这并不总是一个选择,但我想我会把它扔在那里,特别是因为你的 MySQL 示例似乎是专门为拆分为 2 个部分而编码的。

标签: mysql sql postgresql


【解决方案1】:

总是花时间浏览手册。

http://www.postgresql.org/docs/current/static/functions-string.html

如果 split_part(string text, delimiter text, field int) 没有做你想做的事(如果我了解你的 MySQL 函数,还有更多),那么你需要解释在哪里以及为什么。

【讨论】:

  • 啊,手册似乎将“字符串函数和运算符”与“其他字符串函数”分开,这是我显然忽略的后者。谢谢。
  • @jesse_galley: "Some of them are used internally to implement the SQL-standard string functions listed in Table 9-5." [强调我的]。因此,第一个列表旨在涵盖 SQL 标准指定的函数,而第二个列表旨在涵盖 PostgreSQL 扩展。
  • 虽然 SPLIT_PART 解决了上面显示的示例,但它不是模拟 MySQL 的 SUBSTRING_INDEX 的工具,因为 SUBSTRING_INDEX 返回指定出现次数的分隔符左侧或右侧的子字符串。当遇到任意数量的分隔符时,如何模拟它的行为?例如,从 URL 的较大部分中选择域;例如:从“sports.adventures.hobbies.domain.com”和“pets.domain.com”中选择“domain.com”?为了在 PostgreSQL 中解决这个问题,我们可能需要 reg 表达式或组合各种字符串函数。
  • split_part 不能完全替代 SUBSTRING_INDEX 还有另一个原因:字段必须大于零。 MySQL 允许负值从字符串末尾相对拆分。
【解决方案2】:

这是我在 PostgreSQL 中实现(或模拟)MySQL 的 substring_index() 的方法

CREATE OR REPLACE FUNCTION public.substring_index (
  str text,
  delim text,
  count integer = 1,
  out substring_index text
)
RETURNS text AS
$body$
BEGIN
  IF count > 0 THEN
    substring_index = array_to_string((string_to_array(str, delim))[:count], delim);
  ELSE
    DECLARE
      _array TEXT[];
    BEGIN
      _array = string_to_array(str, delim);
      substring_index = array_to_string(_array[array_length(_array, 1) + count + 1:], delim);    
    END;  
  END IF;
END;
$body$
LANGUAGE 'plpgsql'
IMMUTABLE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 5;

这是 mysql 文档中的示例;

postgres=# SELECT substring_index('www.mysql.com', '.', 2);
 substring_index
-----------------
 www.mysql
(1 row)

postgres=# SELECT substring_index('www.mysql.com', '.', -2);
 substring_index
-----------------
 mysql.com
(1 row)

【讨论】:

    猜你喜欢
    • 2013-07-23
    • 1970-01-01
    • 2023-01-17
    • 2021-12-21
    • 2021-07-20
    • 2012-08-17
    • 2014-07-14
    • 2019-02-17
    • 2011-10-16
    相关资源
    最近更新 更多