如何顯示帶數字的並且在 MySQL 中設定的 varchar 中的最高值?


為此,你需要將 varchar 值轉換為 INTEGER。

我們先建立一個表 −

mysql> create table DemoTable765 (ItemPrice varchar(200));
Query OK, 0 rows affected (0.52 sec)

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

mysql> insert into DemoTable765 values('567.00');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable765 values('1089.00');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable765 values('540.00');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable765 values('788.00');
Query OK, 1 row affected (0.39 sec)

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

mysql> select *from DemoTable765;

這將產生以下輸出 -

+-----------+
| ItemPrice |
+-----------+
| 567.00    |
| 1089.00   |
| 540.00    |
| 788.00    |
+-----------+
4 rows in set (0.00 sec)

以下是顯示帶數字的字串中最高值的查詢。我們使用了 CAST() 將其轉換為 INTEGER −

mysql> select *from DemoTable765 order by cast(ItemPrice AS SIGNED INTEGER) DESC;

這將產生以下輸出 -

+-----------+
| ItemPrice |
+-----------+
| 1089.00   |
| 788.00    |
| 567.00    |
| 540.00    |
+-----------+
4 rows in set, 4 warnings (0.00 sec)

更新於: 03-09-2019

75 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.