简体   繁体   English

如何在MySQL中更新和插入多行

[英]How to update and insert multiple row in Mysql

I have an array $feedArray something like this 我有一个数组$feedArray这样的东西

Array ( [0] => array('id'=>'3','val=>'Renta Comerciales'),
        [1] => array('id'=>'4','val=>'Renta de Casas'),
        [2] => array('id'=>'6','val=>'Venta de Casas '),
        [3] => array('id'=>'7','val=>'Venta de Departamentos'), 
        [4] => array('id'=>'1','val=>'Venta de Terrenos') ) 

I have to make two query in a table called categoriasPlantilla 我必须在名为categoriasPlantilla的表中进行两个查询

Then table has three columns 然后表格有三列

plantilla ,categoriaFeed, id

I have to check if the val of the array exist in the categoriafeed column of table then I have to update that id with the array id 我必须检查表的categoriafeed列中是否存在数组的val ,然后必须使用数组id更新该id

if not then I have to insert a new row to the table 如果没有,那么我必须在表中插入新行

The value for plantilla is 11 Plantilla的价值是11

I know how to do multiple insert in one query but how I can build such a query for update ? 我知道如何在一个查询中进行多次插入,但是如何建立这样的查询进行更新?

To begin I was thinking of doing something like this 首先,我正在考虑做这样的事情

$sql = "INSERT INTO categoriaplantilla (plantilla,categoriafeed,id) values ";
    $sqlUpdate = "UPDATE  categoriaplantilla ";
    foreach($feedArray as $key =>$val)
    {
        $sqlUpdate . =;
        $sql . = ;
    }

Please help me out 请帮帮我

Thanks & Regards 感谢和问候

You can't update rows set in one query. 您无法更新在一个查询中设置的行。 One row - one query. 一行-一个查询。 Also, you can use something like this 另外,您可以使用类似这样的内容

UPDATE table SET field1='aaa', field2='bbb' where id IN (1, 2,4).

But this query will set equal data to each row. 但是此查询将为每一行设置相等的数据。 Sometimes I use this one 有时候我用这个

INSERT INTO table(field1, field2) VALUES ('aaa', 'bbb')
ON DUPLICATE KEY
UPDATE SET field1='aaa', field2='bbb' where id IN (1, 2,4)

ps if you are use PK in your query, you may forgot about preformance almost in any case. ps,如果您在查询中使用PK,则几乎在任何情况下都可能忘记性能。

Most solutions used are based on checking primary keys, like INSERT INTO... ON DUPLICATE KEY , or REPLACE INTO... . 使用的大多数解决方案都基于检查主键,例如INSERT INTO... ON DUPLICATE KEYREPLACE INTO...

But, if you are checking non-primary keys which is column categoriaFeed as you mentioned, I suggest you can create a mysql procedure to do that using mysql functions. 但是,如果您正在检查非主键(即您提到的列categoriaFeed ,则建议您可以创建一个mysql过程以使用mysql函数来执行该操作。

The basic code should be like this: 基本代码应如下所示:

DROP PROCEDURE IF EXISTS func;

DELIMITER //
CREATE PROCEDURE func()
BEGIN
    IF '123' in ('Renta Comerciales', 'Renta de Casas', 'Venta de Casas') THEN
        UPDATE `may` SET `fone` = 'aaaaa' WHERE `id` = 1;
    ELSE
        INSERT INTO `may` VALUES ('1','2','3');
    END IF;
END//
DELIMITER ;

CALL func();

The 123 equals to your val , you can check if it's in your given array. 123等于val ,您可以检查它是否在给定的数组中。 Then you can just update or insert data by the if-statement . 然后,您可以通过if-statement更新或插入数据。

This is probably an useful way to solve your problem. 这可能是解决问题的有用方法。 I'm in GMT+8 time zone and I'm gonna sleep now, it's 23:00PM. 我在GMT+8时区,现在要睡觉了,现在是23:00 PM。 I'll check your replies tomorrow to see what I can help then. 明天我将检查您的答复,以帮助您。

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

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