让我们首先创建一个表-
mysql> create table DemoTable( Value int, Value2 varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(10,'999.999.999.999'); mysql> insert into DemoTable values(20,'888.888.888.888'); mysql> insert into DemoTable values(30,'999.999.999.999');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-------+-----------------+ | Value | Value2 | +-------+-----------------+ | 10 | 999.999.999.999 | | 20 | 888.888.888.888 | | 30 | 999.999.999.999 | +-------+-----------------+ 3 rows in set (0.00 sec)
以下是根据相应的VARCHAR记录查找平均值的查询:
mysql> select Value2,avg(Value) from DemoTable group by Value2;
这将产生以下输出-
+-----------------+------------+ | Value2 | avg(Value) | +-----------------+------------+ | 999.999.999.999 | 20.0000 | | 888.888.888.888 | 20.0000 | +-----------------+------------+ 2 rows in set (0.06 sec)