Có thể so sánh bằng cách dùng CAST() chuyển sang số. Tạo bảng:
mysql> create table DemoTable -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentScore varchar(100) -> ); Query OK, 0 rows affected (0.66 sec)
Chèn dữ liệu:
mysql> insert into DemoTable(StudentScore) values('90'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(StudentScore) values('100'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentScore) values('56'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(StudentScore) values('98'); Query OK, 1 row affected (0.13 sec)Hiển thị dữ liệu:
mysql> select *from DemoTable;
Kết quả
+-----------+--------------+ | StudentId | StudentScore | +-----------+--------------+ | 1 | 90 | | 2 | 100 | | 3 | 56 | | 4 | 98 | +-----------+--------------+ 4 rows in set (0.00 sec)
Truy vấn so sánh sau khi CAST sang SIGNED:
mysql> select *from DemoTable where CAST(StudentScore AS SIGNED) > 91;
Kết quả
+-----------+--------------+ | StudentId | StudentScore | +-----------+--------------+ | 2 | 100 | | 4 | 98 | +-----------+--------------+ 2 rows in set (0.00 sec)