如何從 MySQL 中的 varchar 列獲取大於某個特定值的值?


由於想要獲取其值大於特定值的列是 VARCHAR,因此請使用 CAST() 函式。例如,從具有 varchar 值的列中獲取大於 999 的值。

讓我們首先建立一個表 -

mysql> create table DemoTable
(
   Value varchar(100)
);
Query OK, 0 rows affected (1.02 sec)

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

mysql> insert into DemoTable values('900');
Query OK, 1 row affected (0.49 sec)
mysql> insert into DemoTable values('1090');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('860');
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable values('12345');
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable values('908345');
Query OK, 1 row affected (0.16 sec)

使用 select 語句從表中顯示所有記錄 -

mysql> select *from DemoTable;

這將生成以下輸出 -

+--------+
| Value  |
+--------+
| 900    |
| 1090   |
| 860    |
| 12345  |
| 908345 |
+--------+
5 rows in set (0.00 sec)

以下是從 varchar 列獲取大於特定值的值的查詢 -

mysql> select max(cast(Value AS SIGNED)) from DemoTable;

這將生成以下輸出 -

+----------------------------+
| max(cast(Value AS SIGNED)) |
+----------------------------+
|                     908345 |
+----------------------------+
1 row in set (0.05 sec)

更新日期: 24-Sep-2019

393 次瀏覽

開啟您的 職業

透過完成該課程來獲得認證

開始行動
廣告
© . All rights reserved.