用于按数值差异显示记录的 MySQL 查询?
使用 ORDER BY 并设置差值以显示按数值差排序的记录。以下是语法 −
select *from yourTableName order by (yourIntegerColumnName1 - yourIntegerColumnName2);
我们首先创建一个表 −
mysql> create table DemoTable1313 -> ( -> Name varchar(20), -> Score1 int, -> Score2 int -> ); Query OK, 0 rows affected (3.48 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1313 values('Chris',40,60); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable1313 values('David',70,50); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1313 values('Adam',35,30); Query OK, 1 row affected (0.18 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable1313;
输出
+-------+--------+--------+ | Name | Score1 | Score2 | +-------+--------+--------+ | Chris | 40 | 60 | | David | 70 | 50 | | Adam | 35 | 30 | +-------+--------+--------+ 3 rows in set (0.00 sec)
以下是对按数值差排序的查询 −
mysql> select *from DemoTable1313 -> order by (Score1-Score2);
输出
+-------+--------+--------+ | Name | Score1 | Score2 | +-------+--------+--------+ | Chris | 40 | 60 | | Adam | 35 | 30 | | David | 70 | 50 | +-------+--------+--------+ 3 rows in set (0.00 sec)
广告