Computer >> Computer tutorials >  >> Programming >> MySQL

Set multiple values for custom columns in MySQL?


For this, you can use UNION ALL. Let us first create a table −

mysql> create table DemoTable1987
   (
   UserValue int
   );
Query OK, 0 rows affected (2.90 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1987 values(4);
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable1987 values(5);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1987 values(6);
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable1987 values(7);
Query OK, 1 row affected (0.09 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1987;

This will produce the following output −

+-----------+
| UserValue |
+-----------+
|         4 |
|         5 |
|         6 |
|         7 |
+-----------+
4 rows in set (0.00 sec)

Here is the query to set multiple values for custom columns −

mysql> select UserValue,(UserValue*100) as NewValue from DemoTable1987
     union all
     select UserValue,(UserValue*50) as NewValue from DemoTable1987
     order by UserValue;

This will produce the following output −

+-----------+----------+
| UserValue | NewValue |
+-----------+----------+
|         4 |      400 |
|         4 |      200 |
|         5 |      500 |
|         5 |      250 |
|         6 |      300 |
|         6 |      600 |
|         7 |      350 |
|         7 |      700 |
+-----------+----------+
8 rows in set (0.14 sec)