我正在将行插入表中。我有Program_ID、Trip_ID、P_username基本上所有候选键来唯一标识任何记录。
我将在插入的新行上设置 selected = 1,但同时我希望在表中与Program_ID 和 Trip_ID组合不匹配的所有行上将 selected 设置为 0。
如何在 1 个查询中完成此操作,最好使用活动记录。?
在这种情况下,新参与者是Mike3、Andy2
foreach( $all_participants as $single_participant )
{
$this->db->insert('Itinerary', $single_participant);
}
伪代码
Select Selected from Itinerary
where Program_ID ! = $current_program ID and Trip_ID != $current_trip_id
set selected = '0';
行程表:
---------------------------------------------
| Program_ID | Trip_ID |P_Username|Selected |
---------------------------------------------
| f33r3 | F3231 | Andy2 | 1 | <- new row selected = 1
---------------------------------------------
| f33r3 | F3231 | Mike3 | 1 | <- new row selected = 1
---------------------------------------------
| YUAd4 | F3231 | Jane12 | 1 | <- old row set selected = 0
---------------------------------------------
| Hl341 | F3231 | Mike44 | 0 | <- Keep at 0
--------------------------------------------