MySQL根据特定条件选择数据和拆分结果

时间:2016-10-21 15:19:50

标签: php mysql

我的桌子上有出价。

public class NewsAdapter extends ArrayAdapter<Articles_Map> {
    Context mContext;

    public NewsAdapter(Context c, int resource) {
        super(c, resource);
        this.mContext = c;
    }

    protected void doAdd(Articles_Map another) {
        super.add(another);
    }

    public void addAll(List<Articles_Map> others) {
        for (Articles_Map a : others) {
            this.doAdd(a);
        }
        this.sort(byPublishedAtComparator);
    }

    private static final Comparator<Articles_Map> byPublishedAtComparator =
            new Comparator<Articles_Map>() {
                @Override
                public int compare(Articles_Map o1, Articles_Map o2) {
                    // needs further testing in case of nulls
                    if (o1.publishedAt == null || o2.publishedAt == null) {
                        return 0;
                    }

                    return o1.publishedAt.compareTo(o2.publishedAt);
                }
            };
}

我需要输出一个auctionID(这次是3),首先是pr,第二个是bidTime。

+----+-----------+----------+---------+--------------------------+------+
| id | auctionID | bidderID | amount  | bidTime                  | pr   |
+----+-----------+----------+---------+--------------------------+------+
|  5 |         3 |        3 | 1000000 | 2016-10-18 21:59:36.0000 |   25 |
|  6 |         4 |        1 |  100000 | 2016-10-20 23:29:12.0000 |   30 |
|  7 |         4 |        2 |  100000 | 2016-10-20 23:29:25.3352 |   25 |
|  8 |         4 |        3 |  500000 | 2016-10-20 23:30:09.0000 |   50 |
|  9 |         3 |        2 |  250000 | 2016-10-20 23:54:26.0000 |   25 |
| 10 |         3 |        1 | 5000000 | 2016-10-21 03:56:00.0000 |   60 |
+----+-----------+----------+---------+--------------------------+------+

到目前为止一切顺利。

现在我需要根据金额将此选项拆分为中标和非中标。让我们说拍卖3的最大总金额设置为5500000.在这种情况下,id = 5的出价必须分成两半。

示例输出,我正在寻找(中标与失去的出价以及所需金额的分割,在本例中为5500000):

SELECT * FROM table WHERE auctionID = 3 ORDER BY pr DESC, bidTime ASC;
+----+-----------+----------+---------+--------------------------+------+
| id | auctionID | bidderID | amount  | bidTime                  | pr   |
+----+-----------+----------+---------+--------------------------+------+
| 10 |         3 |        1 | 5000000 | 2016-10-21 03:56:00.0000 |   60 |
|  5 |         3 |        3 | 1000000 | 2016-10-18 21:59:36.0000 |   25 |
|  9 |         3 |        2 |  250000 | 2016-10-20 23:54:26.0000 |   25 |
+----+-----------+----------+---------+--------------------------+------+

我知道用PHP / MySQL做这件事,但我只想在MySQL中做这件事。

谢谢

0 个答案:

没有答案