在 MySQL 中为自定义列设置多个值?
mysqlmysqli database更新于 2024/12/10 9:16:17
为此,您可以使用 UNION ALL。让我们首先创建一个表 −
mysql> create table DemoTable1987 ( UserValue int ); Query OK, 0 rows affected (2.90 sec)
使用 insert 命令在表中插入一些记录 −
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)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1987;
这将产生以下输出 −
+-----------+ | UserValue | +-----------+ | 4 | | 5 | | 6 | | 7 | +-----------+ 4 rows in set (0.00 sec)
这是为自定义列设置多个值的查询 −
mysql> select UserValue,(UserValue*100) as NewValue from DemoTable1987 union all select UserValue,(UserValue*50) as NewValue from DemoTable1987 order by UserValue;
这将产生以下输出 −
+-----------+----------+ | 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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

