0

从我的表中插入/更新布尔值的最佳解决方案是什么。

每周或每月,point可以更改数量,例如当前数据:

+----+----------+-----------+--------------+-------+--------+
| id | phone_id | tarrif_id | affiliate_id | point | active |
+----+----------+-----------+--------------+-------+--------+
|  2 |        6 |         2 |            3 |   7.3 |      0 |
|  3 |        6 |         2 |            3 |   8.5 |      0 |
|  4 |        6 |         2 |            3 |  12.5 |      0 |
|  5 |        6 |         2 |            3 |   3.5 |      1 |
|  6 |       20 |         2 |            3 |  10.5 |      1 |
+----+----------+-----------+--------------+-------+--------+

我会插入一个新的记录phone_idtarrif_idaffiliate_id带有一个新的点并禁用其他记录active=0

新点更改,例如:

+----+----------+-----------+--------------+-------+--------+
| id | phone_id | tarrif_id | affiliate_id | point | active |
+----+----------+-----------+--------------+-------+--------+
|  2 |        6 |         2 |            3 |   7.3 |      0 |
|  3 |        6 |         2 |            3 |   8.5 |      0 |
|  4 |        6 |         2 |            3 |  12.5 |      0 |
|  5 |        6 |         2 |            3 |   3.5 |      0 |
|  6 |       20 |         2 |            3 |  10.5 |      0 |
|  7 |        6 |         2 |            3 |  20.2 |      1 |
|  8 |       20 |         2 |            3 |  33.7 |      1 |
+----+----------+-----------+--------------+-------+--------+
4

1 回答 1

0

你可以这样做:

-- 1. Disable other records
UPDATE point SET active = 0;

-- 2. Insert new rows
INSERT INTO point (phone_id, tarrif_id, affiliate_id, point, active)
SELECT phone_id, tarrif_id, affiliate_id, /* Your computation here */, 1
FROM point
GROUP BY phone_id, tarrif_id, affiliate_id
WHERE active = 0;

您仍然需要知道如何计算新point值(在您的示例中为 20.2 和 33.7)。

于 2012-09-21T12:25:18.710 回答