简体   繁体   English

MySQL变量值作为选择子句中的列

[英]Mysql variable value as column in select clause

I am assigning a variable through below select query output. 我通过下面的选择查询输出分配一个变量。

 select group_concat(COLUMN_NAME) 
  from 
    ( select  distinct COLUMN_NAME 
       FROM information_schema.`COLUMNS` C  
     WHERE table_name = 'ADM_METERQUEUE' 
        AND COLUMN_NAME LIKE '%maxrate%' 
      order 
         by 1 desc 
      limit 5) as ids 
  INTO @COLUMNS

Assigned variable has output like below. 分配的变量具有如下输出。

select @COLUMNS

o/p :: maxrate23,maxrate22,maxrate21,maxrate20,maxrate19 o / p :: maxrate23,maxrate22,maxrate21,maxrate20,maxrate19

When I am using in select clause i am getting like below. 当我在select子句中使用时,我将如下所示。

select @COLUMNS from ADM_METERQUEUE where meterqueueid=38

o/p :: maxrate23,maxrate22,maxrate21,maxrate20,maxrate19 o / p :: maxrate23,maxrate22,maxrate21,maxrate20,maxrate19

I want to achieve like below, please let me know how to do it. 我要实现以下目标,请让我知道该怎么做。

Desired Output:: 所需的输出:

select maxrate23,maxrate22,maxrate21,maxrate20,maxrate19 from ADM_METERQUEUE where meterqueueid=38;

+-----------+-----------+-----------+-----------+-----------+
| maxrate23 | maxrate22 | maxrate21 | maxrate20 | maxrate19 |
+-----------+-----------+-----------+-----------+-----------+
|       2   |   7       |  4        |    4      |  1        |
+-----------+-----------+-----------+-----------+-----------+

select group_concat(COLUMN_NAME)    from      ( select  distinct COLUMN_NAME         FROM information_schema.`COLUMNS` C        WHERE table_name = 'ADM_METERQUEUE'          AND COLUMN_NAME LIKE '%maxrate%'        order           by 1 desc        limit 5) as ids    INTO @COLUMNS;

PREPARE stmt FROM 'select ? from ADM_METERQUEUE';

 EXECUTE stmt USING @COLUMNS;

o/p :: maxrate23,maxrate22,maxrate21,maxrate20,maxrate19 o / p :: maxrate23,maxrate22,maxrate21,maxrate20,maxrate19

Still i am getting same column names as output Server version: 5.6.37-82.2-56-log Percona XtraDB Cluster (GPL), Release rel82.2, Revision 114f2f2, WSREP version 26.21, wsrep_26.21 我仍然获得与输出服务器版本相同的列名:5.6.37-82.2-56-log Percona XtraDB群集(GPL),版本rel82.2,修订版114f2f2,WSREP版本26.21,wsrep_26.21

You are very nearly there, but this section from the docs should complete the picture 您已经很近了,但是文档中的这一部分应该使图片更完整

Statement names are not case-sensitive. 语句名称不区分大小写。 preparable_stmt is either a string literal or a user variable that contains the text of the SQL statement. preparable_stmt是字符串文字或包含SQL语句文本的用户变量。 The text must represent a single statement, not multiple statements. 文本必须表示单个语句,而不是多个语句。 Within the statement, ? 在语句中,? characters can be used as parameter markers to indicate where data values are to be bound to the query later when you execute it. 字符可用作参数标记,以指示稍后在执行查询时将数据值绑定到查询的位置。 The ? characters should not be enclosed within quotation marks, even if you intend to bind them to string values. 即使您打算将字符绑定到字符串值,也不应将这些字符括在引号中。 Parameter markers can be used only where data values should appear, not for SQL keywords, identifiers, and so forth . 参数标记只能用于应该显示数据值的位置,而不能用于SQL关键字,标识符等

In other words, you can use a data value such as meterqueueid as a bound variable but not the column identifiers collected in @COLUMNS . 换句话说,您可以使用诸如meterqueueid类的数据值作为绑定变量,但不能使用@COLUMNS收集的列标识符。

Assuming that meterqueueid is also being sourced from another variable, then something like this should work 假设meterqueueid也从另一个变量中获取,那么类似的东西应该可以工作

SET @mqid = 38;
SET @sql = CONCAT('SELECT ', @COLUMNS, ' FROM ADM_METERQUEUE where meterqueueid=?');

PREPARE stmt FROM @sql;
EXECUTE stmt USING @mqid;
DEALLOCATE PREPARE stmt; 

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

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