1

私がやろうとしているのは、いくつかの列を他のテーブルにコピーすることです。obj_id

UPDATE obj_object
INNER JOIN obj_extract as address ON (obj_object.id = address.obj_id AND address.category_id = 1)
INNER JOIN obj_extract as city ON (obj_object.id = city.obj_id AND city.category_id = 2)
INNER JOIN obj_extract as country ON (obj_object.id = country.obj_id AND country.category_id = 3)
INNER JOIN obj_extract as phone ON (obj_object.id = phone.obj_id AND phone.category_id = 4)
SET obj_object.address = address.info,
    obj_object.city = city.info,
    obj_object.country = country.info,
    obj_object.phone = phone.info

しかし、いくつかのobj_extract行は同じです。同じものを含むレコードに同じ行が複数あるcategory_idかどうかを数える必要があります。そうであれば、これらのエントリを作成してテーブルの列にコピーする必要があります。表の例:obj_idcategory_idconcatobj_object

+----+--------------+---------+---------+
| ID |  category_id |  info   | obj_id  |
+----+--------------+---------+---------+
|  1 |        1     |   test  |    2    |
+----+--------------+---------+---------+
|  2 |        1     |  test1  |    2    |
+----+--------------+---------+---------+
|  3 |        2     |   test2 |    2    |
+----+--------------+---------+---------+

私は次のようになる必要があります:

 obj_object.address = 'test - test1', // // value from address.info
 obj_object.city = 'test2', // value from city.info
4

1 に答える 1

1

あなたは使用を検討する必要がありますGROUP_CONCAT

UPDATE obj_object o
SET o.address = (SELECT GROUP_CONCAT(info) 
                FROM obj_extract o2 
                WHERE o.id = o2.obj_id AND o2.category_id = 1),
   o.city = (SELECT GROUP_CONCAT(info) 
            FROM obj_extract o3 
            WHERE o.id = o3.obj_id AND o3.category_id = 2),
   o.country = (SELECT GROUP_CONCAT(info) 
               FROM obj_extract o4 
               WHERE o.id = o4.obj_id AND o4.category_id = 3),
   o.phone = (SELECT GROUP_CONCAT(info) 
             FROM obj_extract o5 
             WHERE o.id = o5.obj_id AND o5.category_id = 4)

幸運を。

于 2013-02-04T14:19:38.000 に答える