2016-09-07 30 views
0

我有两个字段数量和价格的一个表,我想乘这两个字段值: 这里就是我想这是有可能使用MYSQL如何使用Realm查询每行的乘法2单元?

Quantity| Price | Total 

6  | 2 | null // should be 12 
2  | 10 | null // should be 20 

以上是可以做到用

SELECT 
    quantity, Price, 
    quantity* Price as 'Total' 
FROM myTable 

我想要使用领域查询相同的结果..我怎么能实现这一点..任何帮助将appriciated .. 谢谢。

+0

哪个平台? Android或Swift或React-Native或...? – EpicPandaForce

+0

for android .... –

回答

0

使用Realm for Android,您需要添加一个新字段和自定义设置器。

该解决方案支持0.88.0+

public class Something extends RealmObject { 
    private long quantity; 
    private long price; 
    private long total; 

    public long getQuantity() { 
     return quantity; 
    } 

    public long getPrice() { 
     return price; 
    } 

    public long getTotal() { 
     return total; 
    } 

    public void setQuantity(long quantity) { 
     this.quantity = quantity; 
     this.total = this.quantity * this price; 
    } 

    public void setPrice(long price) { 
     this.price = price; 
     this.total = this.quantity * this.price; 
    } 

    public void setTotal(long total) { // preferably don't use 
     this.total = total; 
    } 
} 
+0

此问题与[此github帮助问题]非常相似(https://github.com/realm/realm-java/issues/3378) – EpicPandaForce

+0

谢谢@EpicPandaForce –

+0

哎呦我乘以总数而不是价格。虽然修复了它 – EpicPandaForce