简体   繁体   English

使用MySQL函数或过程提取SELECT表达式

[英]Extract SELECT expression using MySQL function or procedure

I'm interested in simplifying an SQL query/view by extracting the select expression ( select_expr from the MySQL docs ). 我对通过提取选择表达式(来自MySQL文档的 select_expr )简化SQL查询/视图感兴趣。 Each of the select_expr is essentially duplicated with a small amount of variation that could be extracted into variables. 每个select_expr本质上都是重复的,只有少量变化可以复制到变量中。

For example, here is an existing query/view. 例如,这是一个现有的查询/视图。

CREATE OR REPLACE VIEW my_view AS
SELECT

  json_unquote(json_extract(sr.response, concat(SUBSTRING_INDEX(json_unquote(
    JSON_SEARCH(mt.response, 'one', 'pref.field_1', NULL, '$.f[*].q')), '.', 2), 
    '.', 'value'))) AS field_1,

  json_unquote(json_extract(sr.response, concat(SUBSTRING_INDEX(json_unquote(
    JSON_SEARCH(mt.response, 'one', 'pref.field_2', NULL, '$.f[*].q')), '.', 2), 
    '.', 'value'))) AS field_2,

  json_unquote(json_extract(sr.response, concat(SUBSTRING_INDEX(json_unquote(
    JSON_SEARCH(mt.response, 'one', 'pref.field_3', NULL, '$.f[*].q')), '.', 2), 
    '.', 'value'))) AS field_3,

FROM my_table mt;

The variable bits are: field_1 , field_2 , and field_3 . 可变位是: field_1field_2field_3

In theory, this is what I would like to do: 从理论上讲,这是我想做的:

CREATE OR REPLACE VIEW my_view AS
SELECT

  get_select_expr('field_1') AS field_1,
  get_select_expr('field_2') AS field_2,
  get_select_expr('field_3') AS field_3,

FROM my_table mt;

I've been trying something like the following, but not sure how to get the select_expr to evaluate. 我一直在尝试以下类似的方法,但是不确定如何获取select_expr进行评估。 It makes sense that it's returning a string, but I can't figure out how to get it to evaluate. 返回一个字符串是有意义的,但是我不知道该如何评估它。 Maybe I should be using a procedure, but this is where my MySQL knowledge breaks down. 也许我应该使用一个过程,但这是我的MySQL知识崩溃的地方。

DROP FUNCTION IF EXISTS get_select_expr;
CREATE FUNCTION get_select_expr (field_name VARCHAR(255))
RETURNS VARCHAR(255) DETERMINISTIC
RETURN concat('json_unquote(json_extract(mt.response, concat(
    SUBSTRING_INDEX(json_unquote(JSON_SEARCH(mt.response, 
    \'one\', \'pref.', field_3, '', NULL, \'$.f[*].q\')), 
    \'.\', 2), \'.\', \'value\')))');

SELECT get_select_expr('field_1') AS field_1 FROM my_table;

I've gone through all of the suggested similar questions, but not finding what I need. 我经历了所有建议的类似问题,但没有找到我需要的东西。 Any idea where I may be going wrong, or pointers? 知道我可能会出错的地方或指针吗? I'm not even sure I'm searching for the right terms. 我什至不确定我在搜索正确的术语。

You are over complicating the code, there is no need to dynamically generate sql code here and it will not work anyway. 您已经使代码复杂化了,这里不需要动态生成sql代码,反正它也不起作用。

Just create a function that takes a field value and a json field value as parameter and you do not need dynamic sql: 只需创建一个将字段值和json字段值作为参数的函数,就不需要动态sql:

DROP FUNCTION IF EXISTS get_select_expr;
CREATE FUNCTION get_select_expr (field_name VARCHAR(255), json_field_name varchar (255))
RETURNS VARCHAR(255) DETERMINISTIC
RETURN json_unquote(json_extract(field_name, concat(
    SUBSTRING_INDEX(json_unquote(JSON_SEARCH(field_name, 
    'one', 'pref.', json_field_name, '', NULL, '$.f[*].q')), 
    '.', 2), '.', 'value')));

SELECT get_select_expr(my_table.response, 'field_1') AS field_1 FROM my_table;

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

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