如何在选择查询中对正值或负值进行 GROUP BY?
mysqlmysqli database
以下是在选择查询中对正值或负值进行 GROUP BY 的语法:
select *from yourTableName group by -yourColumnName;
首先我们创建一个表:
mysql> create table DemoTable (Value int); Query OK, 0 rows affected (0.60 sec)
下面是使用 insert 命令在表中插入一些记录的查询:
mysql> insert into DemoTable values(-10); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(-20); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(-10); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(-10); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(-20); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(-30); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.12 sec)
以下是使用 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)