如何使用mysql数据库更新条件多行?

时间:2013-07-30 06:18:10

标签: php mysql sql codeigniter

大家好我想问一下。如何使用条件更新数据库中的多个行。 因为我有更新我的表的问题。 这就是我需要做的。我有2个表,一个是ref_cities,一个是ref_draft

在我的ref_cities中,我有一个专栏

mysql> desc ref_cities
+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| id            | int(11)      | NO   | PRI | NULL    |       |
| name          | varchar(255) | YES  |     | NULL    |       |
| province_id   | int(11)      | YES  |     | NULL    |       |
| province_code | varchar(5)   | YES  |     | NULL    |       |
+---------------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

在我的ref_draft中,我有这个

mysql> desc ref_draft;
+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| id           | int(10)     | NO   | PRI | NULL    | auto_increment |
| municipality | varchar(50) | YES  |     | NULL    |                |
| name         | varchar(50) | YES  |     | NULL    |                |
| city_code    | int(11)     | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+
4 rows in set (0.03 sec)

在我的ref_cities值中(我出于示例目的仅列出了一行)

mysql> select * from ref_cities where name like '%manila%';
+------+--------+-------------+---------------+
| id   | name   | province_id | province_code |
+------+--------+-------------+---------------+
| 1976 | Manila |          58 | MNL           |
+------+--------+-------------+---------------+
1 row in set (0.00 sec)

在我的ref_draft值中

mysql> select * from ref_draft where municipality like '%manila%';
+-----+----------------+----------------+-----------+
| id  | municipality   | name           | city_code |
+-----+----------------+----------------+-----------+
|   1 | CITY OF MANILA | Barangay 1     |      NULL |
|   2 | CITY OF MANILA | Barangay 2     |      NULL |
|   3 | CITY OF MANILA | Barangay 3     |      NULL |
|   4 | CITY OF MANILA | Barangay 4     |      NULL |
|   5 | CITY OF MANILA | Barangay 5     |      NULL |
|   6 | CITY OF MANILA | Barangay 6     |      NULL |
|   7 | CITY OF MANILA | Barangay 7     |      NULL |
|   8 | CITY OF MANILA | Barangay 8     |      NULL |
|   9 | CITY OF MANILA | Barangay 9     |      NULL |
|  10 | CITY OF MANILA | Barangay 10    |      NULL |
|  11 | CITY OF MANILA | Barangay 11    |      NULL |
|  12 | CITY OF MANILA | Barangay 12    |      NULL |
|  13 | CITY OF MANILA | Barangay 13    |      NULL |
|  14 | CITY OF MANILA | Barangay 14    |      NULL |
|  15 | CITY OF MANILA | Barangay 15    |      NULL |
|  16 | CITY OF MANILA | Barangay 16    |      NULL |

这应该是样本输出

+-----+----------------+----------------+-----------+
| id  | municipality   | name           | city_code |
+-----+----------------+----------------+-----------+
|   1 | CITY OF MANILA | Barangay 1     |      1976 |
|   2 | CITY OF MANILA | Barangay 2     |      1976 |
|   3 | CITY OF MANILA | Barangay 3     |      1976 |
|   4 | CITY OF MANILA | Barangay 4     |      1976 |
|   5 | CITY OF MANILA | Barangay 5     |      1976 |
|   6 | CITY OF MANILA | Barangay 6     |      1976 |
|   7 | CITY OF MANILA | Barangay 7     |      1976 |
|   8 | CITY OF MANILA | Barangay 8     |      1976 |
|   9 | CITY OF MANILA | Barangay 9     |      1976 |
|  10 | CITY OF MANILA | Barangay 10    |      1976 |
|  11 | CITY OF MANILA | Barangay 11    |      1976 |
|  12 | CITY OF MANILA | Barangay 12    |      1976 |
|  13 | CITY OF MANILA | Barangay 13    |      1976 |
|  14 | CITY OF MANILA | Barangay 14    |      1976 |
|  15 | CITY OF MANILA | Barangay 15    |      1976 |
|  16 | CITY OF MANILA | Barangay 16    |      1976 |

但我知道

+----+----------------+-------------+-----------+
| id | municipality   | name        | city_code |
+----+----------------+-------------+-----------+
|  1 | CITY OF MANILA | Barangay 1  |      1301 |
|  2 | CITY OF MANILA | Barangay 2  |      1301 |
|  3 | CITY OF MANILA | Barangay 3  |      1301 |
|  4 | CITY OF MANILA | Barangay 4  |      1301 |
|  5 | CITY OF MANILA | Barangay 5  |      1301 |
|  6 | CITY OF MANILA | Barangay 6  |      1301 |
|  7 | CITY OF MANILA | Barangay 7  |      1301 |
|  8 | CITY OF MANILA | Barangay 8  |      1301 |
|  9 | CITY OF MANILA | Barangay 9  |      1301 |
| 10 | CITY OF MANILA | Barangay 10 |      1301 |

条件是第一个我将从ref_cities获取名称和id。之后,我将在市政专栏中找到该名称。如果找到,它会将我的ref_cities中的id从我的ref_draft中插入到city_code中。

这是我用于更新行的PHP代码。 (即使用codeigniter)

<?php

$findCityName = "SELECT id,name FROM ref_cities";
$resultFindCity = $this->db->query($findCityName);

foreach($resultFindCity->result_array() as $row){

        $name = $row['name'];
        //$name = 'Manila';

        $update =  array(
            'city_code' => $row['id']
        );
        $this->db->like('municipality','{$name}');
        $this->db->update('ref_draft',$update);

}
?>

1 个答案:

答案 0 :(得分:3)

$this->db->like('municipality','{$name}');

应该是

$this->db->like('municipality',"{$name}");

OR

$this->db->like('municipality',$name);

单个引号变量内部被视为字符串,而不是实际变量。将其括在双引号内或不使用任何引号。