从我的表中插入/更新布尔值的最佳解决方案是什么。
每周或每月,可以更改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_id
,tarrif_id
和affiliate_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 |
+----+----------+-----------+--------------+-------+--------+
答案 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)。