Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Thêm ký tự vào giá trị của cột INT hiện có trong MySQL

Kỹ thuật

Sử dụng hàm CONCAT() để nối chuỗi tiền tố/hậu tố vào giá trị số (số sẽ tự chuyển sang chuỗi).

1. Tạo bảng

mysql> create table DemoTable (
    Amount int
);
Query OK, 0 rows affected (1.44 sec)

2. Chèn dữ liệu

mysql> insert into DemoTable values(709);
mysql> insert into DemoTable values(34560);
mysql> insert into DemoTable values(90854);
mysql> insert into DemoTable values(3456);

3. Xem dữ liệu

mysql> select * from DemoTable;
+--------+
| Amount |
+--------+
| 709    |
| 34560  |
| 90854  |
| 3456   |
+--------+
4 rows in set (0.00 sec)

4. Truy vấn nối chuỗi

mysql> select concat('The Product Amount is=', Amount) as AddingCharacters from DemoTable;

Kết quả:

+-----------------------------+
| AddingCharacters            |
+-----------------------------+
| The Product Amount is=709   |
| The Product Amount is=34560 |
| The Product Amount is=90854 |
| The Product Amount is=3456  |
+-----------------------------+
4 rows in set (0.00 sec)

Áp dụng: Có thể dùng để định dạng hiển thị, thêm tiền tệ, đơn vị, v.v.