如何在 MySQL 计算中使用别名?
mysqlmysqli database
让我们首先创建表。创建表的查询如下 −
mysql> create table AliasDemo −> ( −> Id int −> ); Query OK, 0 rows affected (0.52 sec)
使用 insert 命令在表中插入一些记录。以下是插入记录的查询 −
mysql> insert into AliasDemo values(20); Query OK, 1 row affected (0.45 sec) mysql> insert into AliasDemo values(30); Query OK, 1 row affected (0.20 sec) mysql> insert into AliasDemo values(40); Query OK, 1 row affected (0.83 sec)
现在您可以借助 select 语句显示所有记录 −
mysql> select *from AliasDemo;
以下是输出 −
+------+ | Id | +------+ | 20 | | 30 | | 40 | +------+ 3 rows in set (0.00 sec)
这是在计算中设置别名的查询 −
mysql> select Id,100 as MyNumber ,(select MyNumber)*Id as MultiplyWith100 from AliasDemo;
以下是输出 −
+------+----------+-----------------+ | Id | MyNumber | MultiplyWith100 | +------+----------+-----------------+ | 20 | 100 | 2000 | | 30 | 100 | 3000 | | 40 | 100 | 4000 | +------+----------+-----------------+ 3 rows in set (0.00 sec)
可以单次执行实现,查询如下 −
mysql> select 100 as MyNumber,(select MyNumber)*10 as MultiplyWith100;
以下是输出 −
+----------+-----------------+ | MyNumber | MultiplyWith100 | +----------+-----------------+ | 100 | 1000 | +----------+-----------------+ 1 row in set (0.00 sec)