简体   繁体   English

如何在 MySQL Workbench 中使用 REGEXP_SUBSTR 或 REGEXP_EXTRACT 以及托管在 Google Cloud SQL 上的数据库?

[英]How to use REGEXP_SUBSTR or REGEXP_EXTRACT in MySQL Workbench with database hosted on Google Cloud SQL?

I am working on a MYSQL database which is hosted on Google Cloud SQL. I am working through MySQL Workbench 8.0 CE.我正在处理托管在 Google Cloud SQL 上的 MYSQL 数据库。我正在使用 MySQL Workbench 8.0 CE。

I want to get a substring of the element_name column in the table p0999_pakbon in the schema projects .我想在架构projects的表p0999_pakbon中获取element_name列的 substring。 Element names are like "LVLS 45 Beam 1" and "SPANO 18 Stud 1".元素名称类似于“LVLS 45 Beam 1”和“SPANO 18 Stud 1”。 I want to get: "LVLS 45" and "SPANO 18"我想得到:“LVLS 45”和“SPANO 18”

In an old copy of the database which is hosted on my local machine, the following code works perfectly:在我本地机器上托管的旧数据库副本中,以下代码完美运行:

SELECT REGEXP_SUBSTR(pp.element_name, "[A-Z]+[:space:][0-9]+")
FROM projects.p0999_pakbon pp

This is because REGEXP_SUBSTR is the standard function to get a substring in MySQL. However, this code does not work in my database hosted in Google Cloud SQL. On this page https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#regexp_extract I seem to read that Google Cloud SQL uses REGEXP__EXTRACT instead.这是因为REGEXP_SUBSTR是在 MySQL 中获得 substring 的标准 function。但是,此代码在我托管在 Google Cloud SQL 中的数据库中不起作用。在此页面上https://cloud.google.com/bigreference//doc standard-sql/functions-and-operators#regexp_extract我似乎读到 Google Cloud SQL 使用REGEXP__EXTRACT代替。 But also when I try the following:而且当我尝试以下操作时:

SELECT REGEXP_EXTRACT(pp.element_name, "[A-Z]+[:space:][0-9]+")
FROM projects.p0999_pakbon pp

It does not work.这是行不通的。 It also doesn't work when I change the regex to a very simple one like "A".当我将正则表达式更改为像“A”这样的非常简单的表达式时,它也不起作用。

I am getting these errors:我收到这些错误:
FUNCTION projects.REGEXP_SUBSTR does not exist FUNCTION projects.REGEXP_SUBSTR 不存在
FUNCTION projects.REGEXP_EXTRACT does not exist FUNCTION projects.REGEXP_EXTRACT 不存在

When I type the arguments in the wrong format, I get this error message:当我以错误的格式键入 arguments 时,我收到以下错误消息:
Incorrect parameters in the call to stored fucntion 'REGEXP_EXTRACT'调用存储函数“REGEXP_EXTRACT”时的参数不正确

Does anybody know how to solve my problem?有人知道如何解决我的问题吗? Or maybe there is a workaround?或者也许有解决方法?

Assuming you just want the first two words, you could use SUBSTRING_INDEX here, which should work on most versions of MySQL:假设您只想要前两个词,您可以在此处使用SUBSTRING_INDEX ,它应该适用于 MySQL 的大多数版本:

SELECT SUBSTRING_INDEX('LVLS 45 Beam 1', ' ', 2) AS item
FROM yourTable;

If you also need to assert that the first term contains capitals, with the second digits, you could use REGEXP :如果您还需要断言第一个术语包含大写字母,第二个数字,您可以使用REGEXP

SELECT
    CASE WHEN 'LVLS 45 Beam 1' REGEXP '^[A-Z]+ [0-9]+'
         THEN SUBSTRING_INDEX('LVLS 45 Beam 1', ' ', 2)
         ELSE 'no match' END AS item
FROM yourTable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM