将MIN()函数与SUBSTRING()结合使用,以最小为限,而将MAX()与最大值一起使用。让我们首先创建一个表-
create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Value varchar(100) -> );
使用插入命令在表中插入一些记录-
insert into DemoTable(Value) values('10-20'); insert into DemoTable(Value) values('200-100'); insert into DemoTable(Value) values('780-235');
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+----+---------+ | Id | Value | +----+---------+ | 1 | 10-20 | | 2 | 200-100 | | 3 | 780-235 | +----+---------+ 3 rows in set (0.00 sec)
以下是从带有连字符的字符串分隔的数字子字符串中查找MIN()和MAX()值的查询-
select min(SUBSTRING_INDEX(Value, '-', 1)), -> max(SUBSTRING_INDEX(Value, '-', -1)) -> from DemoTable;
这将产生以下输出-
+-------------------------------------+--------------------------------------+ | min(SUBSTRING_INDEX(Value, '-', 1)) | max(SUBSTRING_INDEX(Value, '-', -1)) | +-------------------------------------+--------------------------------------+ | 10 | 235 | +-------------------------------------+--------------------------------------+ 1 row in set (0.04 sec)