按 GAE 数据存储中的计算列排序
如何对按计算列排序的结果进行查询? (我无法预先计算其他列中的结果来进行排序!)
示例:
在这样的 RDBMS 表中:
TABLE products(
product_id INTEGER NOT NULL,
description VARCHAR(255) NOT NULL,
price DECIMAL(5,2) NOT NULL,
qty INTEGER NOT NULL,
);
我可以执行此查询:
SELECT product_id, (price * qty * {{current_tax}}) AS custom_calc
FROM products
ORDER BY custom_calc DESC;
我的问题是我有一个变量,在本例中{{current_tax}}。这样我就无法预先计算结果来进行排序,因为变量可以随时更改。
对于这样的问题有一个优雅的解决方案吗?
坦克!
How can i make a query with results sorted by a calculated column? (I can not pre calculate the result in other column to do the sorting!)
Example:
In a RDBMS tables like that:
TABLE products(
product_id INTEGER NOT NULL,
description VARCHAR(255) NOT NULL,
price DECIMAL(5,2) NOT NULL,
qty INTEGER NOT NULL,
);
I can perform this query:
SELECT product_id, (price * qty * {{current_tax}}) AS custom_calc
FROM products
ORDER BY custom_calc DESC;
Im my problem i have a variable, in this case {{current_tax}}. In this way I can not pre calculate the result to do the sorting because the variable can change at any time.
there is an elegant solution to problems like this?
Tanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您在文档中看到的,GQL 不支持任何SELECT 语句中的算术运算。
GQL 的存在是为了方便具有 SQL 背景的人,但低级数据存储 API 更像是索引的 k/v 存储。
如果
current_tax
始终是应用于price * qty
的正因子,则无论如何都不会修改排序,因此我建议存储price * qty
作为新的 Product 字段total_price
并为其创建 DESC 索引。As you can see in the documentation, GQL does not support any arithmetic in the SELECT statement.
GQL is there as a convenience for people coming from SQL background, but the low level datastore API is much more like an indexed k/v store.
If
current_tax
is always a positive factor applied onprice * qty
it will not to modify the ordering anyway, so I would recommend storingprice * qty
as a new Product field,total_price
and create a DESC index for it.