mysql 计算结果 排序或 比较_MySQL排序计算

bd96500e110b49cbb3cd949968f18be7.png

Is it possible to sort on a calculation of 2 rows in mySQL? For example, I have 2 rows, lp and ap I'm trying to do something like this:

SELECT * from myTbl WHERE 1 ORDER BY (lp/ap)

Which isn't throwing an error, but its also not sorting by the results of that calculation. Is there a way to do this, or do I have to store lp/ap in the database?

解决方案

Yes, it is possible, and it does work. Check out the following test:

CREATE TABLE a(a INT, b INT);

INSERT INTO a VALUES (1, 1);

INSERT INTO a VALUES (1, 2);

INSERT INTO a VALUES (1, 3);

INSERT INTO a VALUES (1, 4);

INSERT INTO a VALUES (1, 5);

INSERT INTO a VALUES (1, 6);

INSERT INTO a VALUES (2, 1);

INSERT INTO a VALUES (2, 2);

INSERT INTO a VALUES (2, 3);

INSERT INTO a VALUES (2, 4);

INSERT INTO a VALUES (2, 5);

INSERT INTO a VALUES (2, 6);

SELECT a.a, a.b, (a/b) FROM a ORDER BY (a/b);

+------+------+--------+

| a | b | (a/b) |

+------+------+--------+

| 1 | 6 | 0.1667 |

| 1 | 5 | 0.2000 |

| 1 | 4 | 0.2500 |

| 2 | 6 | 0.3333 |

| 1 | 3 | 0.3333 |

| 2 | 5 | 0.4000 |

| 1 | 2 | 0.5000 |

| 2 | 4 | 0.5000 |

| 2 | 3 | 0.6667 |

| 2 | 2 | 1.0000 |

| 1 | 1 | 1.0000 |

| 2 | 1 | 2.0000 |

+------+------+--------+

SELECT a.a, a.b FROM a ORDER BY (a/b); would return the same results.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值