0

大家好,我只是想问一下。如何使用条件更新数据库中的多行。因为我在更新表格时遇到问题。这是我需要做的。我有两张桌子,一张是 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 值中(出于示例目的,我只列出了 1 行)

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);

}
?>
4

1 回答 1

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

应该

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

或者

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

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

于 2013-07-30T06:20:13.887 回答