具有多个相同记录的布尔标志

时间:2012-09-21 12:13:11

标签: mysql database boolean

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

每周或每月,可以更改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 |
+----+----------+-----------+--------------+-------+--------+

1 个答案:

答案 0 :(得分: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)。