简体   繁体   中英

How to pass dynamic parameter to select statement in MySQL?

I have a query in 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. 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.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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