如果尚未存在,我想在表中插入行,否则更新它。 如何在codeignitor中重新编写此查询?
INSERT INTO fa_seat_details (mall_id, cineplex_name, audi_number, number_of_rows, number_of_columns) VALUES(15, "PVR", 1, 18, 16) ON DUPLICATE KEY UPDATE cineplex_name = "some name", number_of_rows=10, number_of_columns=20;
这是表结构,
+-------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| mall_id | int(11) | NO | MUL | NULL | |
| cineplex_name | varchar(64) | NO | | NULL | |
| audi_number | int(11) | NO | | NULL | |
| number_of_rows | int(11) | NO | | NULL | |
| number_of_columns | int(11) | NO | | NULL | |
+-------------------+-------------+------+-----+---------+----------------+
答案 0 :(得分:1)
试试这个
public function FunctionName($value)//pass data to here
{
$query = $this->db->query("SELECT * FROM category WHERE aa = 'bbbbb'");
$result = $query->result_array();
$count = count($result);
if (empty($count)) {
# insert code
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
}
elseif ($count >1 ) {
# Having Multiple data on it
echo "Wrong data selected";
}
else{
#update code
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
}
}
答案 1 :(得分:0)
也许你想要这样的东西:
$data['mall_id'] = 15;
$data['cineplex_name'] = "PVR";
$data['audi_number'] = 1;
$data['number_of_rows'] = 18;
$data['number_of_columns'] = 16;
$sql = $this->db->insert_string('fa_seat_details', $data) . ' ON DUPLICATE KEY UPDATE cineplex_name = "some name", number_of_rows=10, number_of_columns=20';
$this->db->query($sql);