简体   繁体   English

使用 if else 条件将 SQL UPDATE 语句转换为 php (codeigniter)

[英]Convert SQL UPDATE statement to php with if else condition (codeigniter)

I want to make php code with SQL Update Statement in Codeigniter.我想在 Codeigniter 中使用 SQL 更新语句制作 php 代码。

If I execution code in Codeigniter, the data in database will be updated too.如果我在 Codeigniter 中执行代码,数据库中的数据也会更新。

I want to update one column (ID_STATUS) but with several condition.我想更新一列 (ID_STATUS) 但有几个条件。

The connection column ' ID_STATUS ' with column ' lama ' and ' estimasi '连接列 ' ID_STATUS ' 与列 ' lama ' 和 ' estimasi '

My table name is "pelayanan".我的表名是“pelayanan”。

ID_STATUS is PK from table "status". ID_STATUS是来自表“状态”的 PK。

So, Column ID_STATUS in table "pelayanan" is foreign key from table "status".因此,表“pelayanan”中的列ID_STATUS是表“status”中的外键。

I tried with this query, but, It isn't if else condition yet.我试过这个查询,但是,还不是 if else 条件。

condition 1 :条件 1:

UPDATE `dbhpl`.`pelayanan` 
SET `pelayanan`.`ID_STATUS` = '1' 
WHERE `pelayanan`.`LAMA` <> `pelayanan`.`ESTIMASI`;

condition 2:条件2:

UPDATE `dbhpl`.`pelayanan` 
SET `pelayanan`.`ID_STATUS` = '2' 
WHERE `pelayanan`.`LAMA` = `pelayanan`.`ESTIMASI`;

That is the query on mysql.那是对mysql的查询。 But I want to convert that query to php code (Codeigniter).但我想将该查询转换为 php 代码(Codeigniter)。

How come It will be?怎么会呢?

condition 3:条件 3:

  • a.一种。 both LAMA and ESTIMASI are null. LAMAESTIMASI都为空。
  • b.LAMA is null LAMA为空
  • c. C。 ESTIMASI is null ESTIMASI为空

If you want to update all rows in the table, based on the values of LAMA and ESTIMASI , you could do that in one fell swoop with one UPDATE statement.如果您想根据LAMAESTIMASI的值更新表中的所有行,您可以使用一个 UPDATE 语句一举完成。

  UPDATE `dbhpl`.`pelayanan` p 
     SET p.`ID_STATUS`
         = CASE 
             WHEN p.`LAMA`  = p.`ESTIMASI` THEN '2'
             WHEN p.`LAMA` <> p.`ESTIMASI` THEN '1'
             WHEN p.`LAMA` IS NULL AND p.`ESTIMASI` IS NULL THEN p.`ID_STATUS`
             WHEN p.`LAMA` IS NULL THEN p.`ID_STATUS`
             ELSE p.`ID_STATUS`
           END  

Note that assigning the current value of the ID_STATUS column back to the ID_STATUS column results in "no update".请注意,将ID_STATUS列的当前值分配回ID_STATUS列会导致“无更新”。

Since the last two WHEN conditions return the same values as the ELSE, those could be removed.由于最后两个 WHEN 条件返回与 ELSE 相同的值,因此可以删除这些值。 These were included just to illustrate possible handling of condition 3 .包括这些只是为了说明条件 3 的可能处理。

One small difference with this vs. the original is that it will attempt tto update every row in the table, including rows that have a NULL value in LAMA and/or ESTIMASI .这与原始版本的一个小区别是它会尝试更新表中的每一行,包括在LAMA和/或ESTIMASI中具有 NULL 值的行。 That means any UPDATE triggers will be fired for those rows.这意味着将为这些行触发任何 UPDATE 触发器。 To get exactly the same result as the original, you'd need to include a WHERE clause that excludes rows where LAMA is null or ESTIMASI is null.要获得与原始结果完全相同的结果,您需要包含一个WHERE子句,该子句排除LAMA为空或ESTIMASI为空的行。 For example:例如:

   WHERE p.`LAMA` IS NOT NULL
     AND p.`ESTIMASI` IS NOT NULL  

As far as how to accomplish this same thing in PHP, someone else may be able to answer that.至于如何在 PHP 中完成同样的事情,其他人可能能够回答。 Personally, I'd just do it one SQL operation.就个人而言,我只会做一个 SQL 操作。

The ANSI-standard syntax is a bit verbose. ANSI 标准语法有点冗长。 A MySQL specific version that accomplishes the same thing is a bit shorter:完成相同操作的 MySQL 特定版本要短一些:

  UPDATE `dbhpl`.`pelayanan` p 
     SET p.`ID_STATUS` = IFNULL((p.`LAMA`=p.`ESTIMASI`)+1,p.`ID_STATUS`)

FOLLOWUP跟进

If LAMA and ESTIMASI are defined as NOT NULL , then you wouldn't have to deal with condition 3 .如果LAMAESTIMASI被定义为NOT NULL ,那么您就不必处理条件 3 (In the more general case, we don't necessarily have that guarantee, so I think it's better pattern to account for those conditions, even if they won't ever happen in our particular case. (在更一般的情况下,我们不一定有这种保证,所以我认为考虑这些情况是更好的模式,即使它们在我们的特定情况下永远不会发生。

For CodeIgniter ActiveRecord, you'd could try something like this:对于 CodeIgniter ActiveRecord,你可以尝试这样的事情:

 $this->db
   ->set('ID_STATUS', 'IFNULL((`LAMA`=`ESTIMASI`)+1,`ID_STATUS`)', FALSE)
   ->update('`dbhpl`.`pelayanan`');

your first query is你的第一个查询是

UPDATE `dbhpl`.`pelayanan` 
SET `pelayanan`.`ID_STATUS` = '1' 
WHERE `pelayanan`.`LAMA` <> `pelayanan`.`ESTIMASI`;

convert it as follows:将其转换如下:

$update_data=array('ID_STATUS'=>'1');
$this->db->where('LAMA <>','ESTIMASI');
$this->db->update('pelayanan',$update_data);

your second query is你的第二个查询是

UPDATE `dbhpl`.`pelayanan` 
SET `pelayanan`.`ID_STATUS` = '2' 
WHERE `pelayanan`.`LAMA` = `pelayanan`.`ESTIMASI`;

convert it as follows:将其转换如下:

$update_data=array('ID_STATUS'=>'2');
$this->db->where('LAMA','ESTIMASI');
$this->db->update('pelayanan',$update_data);

I have remove database name.我已经删除了数据库名称。 database name will be selected in connection and and did required to mentioned it here.数据库名称将在连接中选择,并且确实需要在此处提及它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM