如何在选择查询中对正值或负值进行GROUP BY?
以下是在正或负值的选择查询中GROUPBY的语法:
select *from yourTableName group by -yourColumnName;
让我们首先创建一个表:
mysql> create table DemoTable (Value int);
以下是使用insert命令在表中插入一些记录的查询:
mysql> insert into DemoTable values(-10); mysql> insert into DemoTable values(-20); mysql> insert into DemoTable values(20); mysql> insert into DemoTable values(10); mysql> insert into DemoTable values(-10); mysql> insert into DemoTable values(-10); mysql> insert into DemoTable values(-20); mysql> insert into DemoTable values(-30); mysql> insert into DemoTable values(30);
以下是使用select命令显示表中记录的查询:
mysql> select *from DemoTable;
这将产生以下输出
+-------+ | Value | +-------+ | -10 | | -20 | | 20 | | 10 | | -10 | | -10 | | -20 | | -30 | | 30 | +-------+ 9 rows in set (0.00 sec)
以下是按正负值分组的查询:
mysql> select *from DemoTable group by -Value;
这将产生以下输出
+-------+ | Value | +-------+ | -10 | | -20 | | 20 | | 10 | | -30 | | 30 | +-------+ 6 rows in set (0.00 sec)