テーブルに行を挿入しています。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
--------------------------------------------