如何使用Realm查询每行2个单元格?

时间:2016-09-07 11:54:09

标签: realm

我有一个表有两个字段数量和价格,我想将两个字段值相乘: 这是我想要的MYSQL

Quantity| Price | Total

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

以上可以使用

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

我希望使用领域查询获得相同的结果..我怎样才能实现这一点..任何帮助都会得到应用... 谢谢。

1 个答案:

答案 0 :(得分: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;
    }
}
相关问题