使用 MySQL SELECT 語句時,在每個值末尾新增百分比 (%) 符號


要新增百分號符號,請使用 CONCAT() 函式。讓我們先建立一個表 -

mysql> create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(100),
   StudentScore int
);
Query OK, 0 rows affected (0.68 sec)

使用 insert 命令在表中插入一些記錄 -

mysql> insert into DemoTable(StudentName,StudentScore) values('John',65);
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable(StudentName,StudentScore) values('Chris',98);
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable(StudentName,StudentScore) values('Robert',91);
Query OK, 1 row affected (0.09 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select *from DemoTable;

這將產生以下輸出 -

+-----------+-------------+--------------+
| StudentId | StudentName | StudentScore |
+-----------+-------------+--------------+
|         1 | John        | 65           |
|         2 | Chris       | 98           |
|         3 | Robert      | 91           |
+-----------+-------------+--------------+
3 rows in set (0.00 sec)

以下是對使用 MySQL SELECT 語句新增百分比 (%) 符號到每個值末尾的查詢 -

mysql> select StudentId,StudentName,concat(StudentScore,'%') AS StudentScore from DemoTable;

這將產生以下輸出 -

+-----------+-------------+--------------+
| StudentId | StudentName | StudentScore |
+-----------+-------------+--------------+
|         1 | John        | 65%          |
|         2 | Chris       | 98%          |
|         3 | Robert      | 91%          |
+-----------+-------------+--------------+
3 rows in set (0.00 sec)

更新日期: 2019 年 9 月 26 日

4000+ 瀏覽

開始你的 職業

透過完成課程獲得認證

開始吧
廣告
© . All rights reserved.