简体   繁体   English

如何在MySQL中传递动态参数来选择语句?

[英]How to pass dynamic parameter to select statement in MySQL?

I have a query in MySQL: 我在MySQL中有一个查询:

select id, 1 from table;

When I run this query I will get the result as below 当我运行此查询时,我将得到如下结果

id   | 1
-----|----
5001 | 1 
5002 | 1 
5003 | 1 

Here I'm passing static value as a parameter to the SQL query. 在这里,我将静态值作为参数传递给SQL查询。 In Place of static value I have a list like 代替静态值,我有一个像

A = [1,2,3]

Size of A is same as the number of rows in the table. A的大小与表中的行数相同。

List elements may not be in the incremental order they may be in any random order elements may have strings also but we will have a list. 列表元素可能不以递增顺序排列,它们可能以任何随机顺序排列,元素也可能包含字符串,但我们将有一个列表。

I want replace static parameter with A. So that it will iterate with list and gives the list elements 我想用A替换静态参数。这样它就会用list进行迭代并给出list元素

I need a query which does like below for example : 我需要一个如下所示的查询:

select id , A from table;

Result : 结果:

id  | A 
----|-----
5001| 1 
5002| 2 
5003| 3 

Can someone help me in generating this query? 有人可以帮助我生成此查询吗?

Try this: 尝试这个:

mysql> select id from new_table;
+----+
| id |
+----+
|  1 |
|  2 |
+----+
2 rows in set (0.00 sec)

mysql> SET @row_number = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> select id, @row_number := @row_number + 1, ELT(@row_number, '1111', 2222) AS A from new_table;
+----+--------------------------------+------+
| id | @row_number := @row_number + 1 | A    |
+----+--------------------------------+------+
|  1 |                              1 | 1111 |
|  2 |                              2 | 2222 |
+----+--------------------------------+------+
2 rows in set (0.00 sec)

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

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