增加MySQL的价值

时间:2016-10-25 10:00:14

标签: mysql

我有一个MySQL表,看起来像:

mysql> select * from llx_societe limit 10 ;
+-------+-----------------------------------------------+------------+--------+---------+---------+--------+--------+---------------------+-------+--------+-------------+------------------+-------------+-------------------------+-----------------------------+-------+------------------+----------------+---------+-----------+------+------+-------+-------+-------------+-----------+--------------------+-------------+-------+-------+------+---------+---------+---------+-----------+---------+-----------+--------------+-------------+-----------+-------------+--------+-------------+------------------+------------------+--------------+--------------------+--------------+---------------+---------------+---------------+---------------+---------------+----------------+----------------+-------------------------+-------------------------+--------------------+-----------+-----------------+-----------------+-----------------+-----------------+---------+-----------------+-------------+-------------------+--------------+------+--------+----------------+-----------------+-----------------+------------------+--------------------+
| rowid | nom                                           | name_alias | entity | ref_ext | ref_int | statut | parent | tms                 | datec | status | code_client | code_fournisseur | code_compta | code_compta_fournisseur | address                     | zip   | town             | fk_departement | fk_pays | phone     | fax  | url  | email | skype | fk_effectif | fk_typent | fk_forme_juridique | fk_currency | siren | siret | ape  | idprof4 | idprof5 | idprof6 | tva_intra | capital | fk_stcomm | note_private | note_public | model_pdf | prefix_comm | client | fournisseur | supplier_account | fk_prospectlevel | fk_incoterms | location_incoterms | customer_bad | customer_rate | supplier_rate | fk_user_creat | fk_user_modif | remise_client | mode_reglement | cond_reglement | mode_reglement_supplier | cond_reglement_supplier | fk_shipping_method | tva_assuj | localtax1_assuj | localtax1_value | localtax2_assuj | localtax2_value | barcode | fk_barcode_type | price_level | outstanding_limit | default_lang | logo | canvas | import_key     | webservices_url | webservices_key | fk_multicurrency | multicurrency_code |
+-------+-----------------------------------------------+------------+--------+---------+---------+--------+--------+---------------------+-------+--------+-------------+------------------+-------------+-------------------------+-----------------------------+-------+------------------+----------------+---------+-----------+------+------+-------+-------+-------------+-----------+--------------------+-------------+-------+-------+------+---------+---------+---------+-----------+---------+-----------+--------------+-------------+-----------+-------------+--------+-------------+------------------+------------------+--------------+--------------------+--------------+---------------+---------------+---------------+---------------+---------------+----------------+----------------+-------------------------+-------------------------+--------------------+-----------+-----------------+-----------------+-----------------+-----------------+---------+-----------------+-------------+-------------------+--------------+------+--------+----------------+-----------------+-----------------+------------------+--------------------+
|  5703 | A.D.P.N.                                      | NULL       |      1 | NULL    | NULL    |      0 |   NULL | 2016-10-25 11:51:59 | NULL  |      1 | 1           | NULL             | NULL        | NULL                    | 5  rue des Narcisses        | 67116 | REICHSTETT       |           NULL |       1 | NULL      | NULL | NULL | NULL  | NULL  |        NULL |      NULL |               NULL | NULL        | NULL  | NULL  | NULL | NULL    | NULL    | NULL    | NULL      |    NULL |         0 | NULL         | NULL        | NULL      | NULL        |      1 |           0 | NULL             | NULL             |         NULL | NULL               |            0 |             0 |             0 |             1 |          NULL |             0 |           NULL |           NULL |                    NULL |                    NULL |               NULL |         1 |               0 |            NULL |               0 |            NULL | NULL    |               0 |        NULL |              NULL | fr_FR        | NULL | NULL   | 20161025092708 | NULL            | NULL            |             NULL | NULL               |
|  5704 | A.P.E.L.E                                     | NULL       |      1 | NULL    | NULL    |      0 |   NULL | 2016-10-25 09:27:08 | NULL  |      1 | 2           | NULL             | NULL        | NULL                    | 0                           | 67000 | Adresse ?        |           NULL |       1 | NULL      | NULL | NULL | NULL  | NULL  |        NULL |      NULL |               NULL | NULL        | NULL  | NULL  | NULL | NULL    | NULL    | NULL    | NULL      |    NULL |         0 | NULL         | NULL        | NULL      | NULL        |      1 |           0 | NULL             | NULL             |         NULL | NULL               |            0 |             0 |             0 |             1 |          NULL |             0 |           NULL |           NULL |                    NULL |                    NULL |               NULL |         1 |               0 |            NULL |               0 |            NULL | NULL    |               0 |        NULL |              NULL | fr_FR        | NULL | NULL   | 20161025092708 | NULL            | NULL            |             NULL | NULL               |
|  5705 | A.Sauv.Nat. BRUMATH & environs KAPFER Gérard  | NULL       |      1 | NULL    | NULL    |      0 |   NULL | 2016-10-25 09:27:08 | NULL  |      1 | 4           | NULL             | NULL        | NULL                    | 2  rue du Maire  Cornélius  | 67170 | BRUMATH          |           NULL |       1 | NULL      | NULL | NULL | NULL  | NULL  |        NULL |      NULL |               NULL | NULL        | NULL  | NULL  | NULL | NULL    | NULL    | NULL    | NULL      |    NULL |         0 | NULL         | NULL        | NULL      | NULL        |      1 |           0 | NULL             | NULL             |         NULL | NULL               |            0 |             0 |             0 |             1 |          NULL |             0 |           NULL |           NULL |                    NULL |                    NULL |               NULL |         1 |               0 |            NULL |               0 |            NULL | NULL    |               0 |        NULL |              NULL | fr_FR        | NULL | NULL   | 20161025092708 | NULL            | NULL            |             NULL | NULL               |
|  5706 | A.S.P.E.E. A. GREINER/MAGNIETTE               | NULL       |      1 | NULL    | NULL    |      0 |   NULL | 2016-10-25 09:27:08 | NULL  |      1 | 5           | NULL             | NULL        | NULL                    | 27 rue des  Alliés          | 67114 | Eschau           |           NULL |       1 | 388685993 | NULL | NULL | NULL  | NULL  |        NULL |      NULL |               NULL | NULL        | NULL  | NULL  | NULL | NULL    | NULL    | NULL    | NULL      |    NULL |         0 | NULL         | NULL        | NULL      | NULL        |      1 |           0 | NULL             | NULL             |         NULL | NULL               |            0 |             0 |             0 |             1 |          NULL |             0 |           NULL |           NULL |                    NULL |                    NULL |               NULL |         1 |               0 |            NULL |               0 |            NULL | NULL    |               0 |        NULL |              NULL | fr_FR        | NULL | NULL   | 20161025092708 | NULL            | NULL            |             NULL | NULL               |
|  5707 | AAPP de Burnaupt HENN René                    | NULL       |      1 | NULL    | NULL    |      0 |   NULL | 2016-10-25 09:27:08 | NULL  |      1 | 6           | NULL             | NULL        | NULL                    | 12  rue de l' Etang         | 68520 | BURNHAUPT LE BAS |           NULL |       1 | NULL      | NULL | NULL | NULL  | NULL  |        NULL |      NULL |               NULL | NULL        | NULL  | NULL  | NULL | NULL    | NULL    | NULL    | NULL      |    NULL |         0 | NULL         | NULL        | NULL      | NULL        |      1 |           0 | NULL             | NULL             |         NULL | NULL               |            0 |             0 |             0 |             1 |          NULL |             0 |           NULL |           NULL |                    NULL |                    NULL |               NULL |         1 |               0 |            NULL |               0 |            NULL | NULL    |               0 |        NULL |              NULL | fr_FR        | NULL | NULL   | 20161025092708 | NULL            | NULL            |             NULL | NULL               |
|  5708 | ABEGG - LITZLER Jacques & Martine             | NULL       |      1 | NULL    | NULL    |      0 |   NULL | 2016-10-25 09:27:08 | NULL  |      1 | 7           | NULL             | NULL        | NULL                    | 16  rue du Muhlberg         | 68730 | BLOTZHEIM        |           NULL |       1 | NULL      | NULL | NULL | NULL  | NULL  |        NULL |      NULL |               NULL | NULL        | NULL  | NULL  | NULL | NULL    | NULL    | NULL    | NULL      |    NULL |         0 | NULL         | NULL        | NULL      | NULL        |      1 |           0 | NULL             | NULL             |         NULL | NULL               |            0 |             0 |             0 |             1 |          NULL |             0 |           NULL |           NULL |                    NULL |                    NULL |               NULL |         1 |               0 |            NULL |               0 |            NULL | NULL    |               0 |        NULL |              NULL | fr_FR        | NULL | NULL   | 20161025092708 | NULL            | NULL            |             NULL | NULL               |
|  5709 | ABT Clémence                                  | NULL       |      1 | NULL    | NULL    |      0 |   NULL | 2016-10-25 09:27:08 | NULL  |      1 | 8           | NULL             | NULL        | NULL                    | 11  rue de Kembs            | 68100 | Mulhouse         |           NULL |       1 | NULL      | NULL | NULL | NULL  | NULL  |        NULL |      NULL |               NULL | NULL        | NULL  | NULL  | NULL | NULL    | NULL    | NULL    | NULL      |    NULL |         0 | NULL         | NULL        | NULL      | NULL        |      1 |           0 | NULL             | NULL             |         NULL | NULL               |            0 |             0 |             0 |             1 |          NULL |             0 |           NULL |           NULL |                    NULL |                    NULL |               NULL |         1 |               0 |            NULL |               0 |            NULL | NULL    |               0 |        NULL |              NULL | fr_FR        | NULL | NULL   | 20161025092708 | NULL            | NULL            |             NULL | NULL               |
|  5710 | ACKER Daniel                                  | NULL       |      1 | NULL    | NULL    |      0 |   NULL | 2016-10-25 09:27:08 | NULL  |      1 | 9           | NULL             | NULL        | NULL                    | 68  rue J. Kablé            | 67000 | Strasbourg       |           NULL |       1 | NULL      | NULL | NULL | NULL  | NULL  |        NULL |      NULL |               NULL | NULL        | NULL  | NULL  | NULL | NULL    | NULL    | NULL    | NULL      |    NULL |         0 | NULL         | NULL        | NULL      | NULL        |      1 |           0 | NULL             | NULL             |         NULL | NULL               |            0 |             0 |             0 |             1 |          NULL |             0 |           NULL |           NULL |                    NULL |                    NULL |               NULL |         1 |               0 |            NULL |               0 |            NULL | NULL    |               0 |        NULL |              NULL | fr_FR        | NULL | NULL   | 20161025092708 | NULL            | NULL            |             NULL | NULL               |
|  5711 | ACKER Emmanuel                                | NULL       |      1 | NULL    | NULL    |      0 |   NULL | 2016-10-25 09:27:08 | NULL  |      1 | 10          | NULL             | NULL        | NULL                    | 34  rue de Soultz           | 67100 | STRASBOURG       |           NULL |       1 | 388846074 | NULL | NULL | NULL  | NULL  |        NULL |      NULL |               NULL | NULL        | NULL  | NULL  | NULL | NULL    | NULL    | NULL    | NULL      |    NULL |         0 | NULL         | NULL        | NULL      | NULL        |      1 |           0 | NULL             | NULL             |         NULL | NULL               |            0 |             0 |             0 |             1 |          NULL |             0 |           NULL |           NULL |                    NULL |                    NULL |               NULL |         1 |               0 |            NULL |               0 |            NULL | NULL    |               0 |        NULL |              NULL | fr_FR        | NULL | NULL   | 20161025092708 | NULL            | NULL            |             NULL | NULL               |
|  5712 | ACKERMANN  Marcelle                           | NULL       |      1 | NULL    | NULL    |      0 |   NULL | 2016-10-25 09:27:08 | NULL  |      1 | 11          | NULL             | NULL        | NULL                    | 5  rue Mittelharth          | 68000 | COLMAR           |           NULL |       1 | NULL      | NULL | NULL | NULL  | NULL  |        NULL |      NULL |               NULL | NULL        | NULL  | NULL  | NULL | NULL    | NULL    | NULL    | NULL      |    NULL |         0 | NULL         | NULL        | NULL      | NULL        |      1 |           0 | NULL             | NULL             |         NULL | NULL               |            0 |             0 |             0 |             1 |          NULL |             0 |           NULL |           NULL |                    NULL |                    NULL |               NULL |         1 |               0 |            NULL |               0 |            NULL | NULL    |               0 |        NULL |              NULL | fr_FR        | NULL | NULL   | 20161025092708 | NULL            | NULL            |             NULL | NULL               |
+-------+-----------------------------------------------+------------+--------+---------+---------+--------+--------+---------------------+-------+--------+-------------+------------------+-------------+-------------------------+-----------------------------+-------+------------------+----------------+---------+-----------+------+------+-------+-------+-------------+-----------+--------------------+-------------+-------+-------+------+---------+---------+---------+-----------+---------+-----------+--------------+-------------+-----------+-------------+--------+-------------+------------------+------------------+--------------+--------------------+--------------+---------------+---------------+---------------+---------------+---------------+----------------+----------------+-------------------------+-------------------------+--------------------+-----------+-----------------+-----------------+-----------------+-----------------+---------+-----------------+-------------+-------------------+--------------+------+--------+----------------+-----------------+-----------------+------------------+--------------------+

我想将rowid = 5703替换为1,将rowid = 5704替换为2等。 为了得到下表:

+-------+-----------------------------------------------+------------+--------+---------+---------+--------+--------+---------------------+-------+--------+-------------+------------------+-------------+-------------------------+-----------------------------+-------+------------------+----------------+---------+-----------+------+------+-------+-------+-------------+-----------+--------------------+-------------+-------+-------+------+---------+---------+---------+-----------+---------+-----------+--------------+-------------+-----------+-------------+--------+-------------+------------------+------------------+--------------+--------------------+--------------+---------------+---------------+---------------+---------------+---------------+----------------+----------------+-------------------------+-------------------------+--------------------+-----------+-----------------+-----------------+-----------------+-----------------+---------+-----------------+-------------+-------------------+--------------+------+--------+----------------+-----------------+-----------------+------------------+--------------------+
| rowid | nom                                           | name_alias | entity | ref_ext | ref_int | statut | parent | tms                 | datec | status | code_client | code_fournisseur | code_compta | code_compta_fournisseur | address                     | zip   | town             | fk_departement | fk_pays | phone     | fax  | url  | email | skype | fk_effectif | fk_typent | fk_forme_juridique | fk_currency | siren | siret | ape  | idprof4 | idprof5 | idprof6 | tva_intra | capital | fk_stcomm | note_private | note_public | model_pdf | prefix_comm | client | fournisseur | supplier_account | fk_prospectlevel | fk_incoterms | location_incoterms | customer_bad | customer_rate | supplier_rate | fk_user_creat | fk_user_modif | remise_client | mode_reglement | cond_reglement | mode_reglement_supplier | cond_reglement_supplier | fk_shipping_method | tva_assuj | localtax1_assuj | localtax1_value | localtax2_assuj | localtax2_value | barcode | fk_barcode_type | price_level | outstanding_limit | default_lang | logo | canvas | import_key     | webservices_url | webservices_key | fk_multicurrency | multicurrency_code |
+-------+-----------------------------------------------+------------+--------+---------+---------+--------+--------+---------------------+-------+--------+-------------+------------------+-------------+-------------------------+-----------------------------+-------+------------------+----------------+---------+-----------+------+------+-------+-------+-------------+-----------+--------------------+-------------+-------+-------+------+---------+---------+---------+-----------+---------+-----------+--------------+-------------+-----------+-------------+--------+-------------+------------------+------------------+--------------+--------------------+--------------+---------------+---------------+---------------+---------------+---------------+----------------+----------------+-------------------------+-------------------------+--------------------+-----------+-----------------+-----------------+-----------------+-----------------+---------+-----------------+-------------+-------------------+--------------+------+--------+----------------+-----------------+-----------------+------------------+--------------------+
|     1 | A.D.P.N.                                      | NULL       |      1 | NULL    | NULL    |      0 |   NULL | 2016-10-25 11:51:59 | NULL  |      1 | 1           | NULL             | NULL        | NULL                    | 5  rue des Narcisses        | 67116 | REICHSTETT       |           NULL |       1 | NULL      | NULL | NULL | NULL  | NULL  |        NULL |      NULL |               NULL | NULL        | NULL  | NULL  | NULL | NULL    | NULL    | NULL    | NULL      |    NULL |         0 | NULL         | NULL        | NULL      | NULL        |      1 |           0 | NULL             | NULL             |         NULL | NULL               |            0 |             0 |             0 |             1 |          NULL |             0 |           NULL |           NULL |                    NULL |                    NULL |               NULL |         1 |               0 |            NULL |               0 |            NULL | NULL    |               0 |        NULL |              NULL | fr_FR        | NULL | NULL   | 20161025092708 | NULL            | NULL            |             NULL | NULL               |
|     2 | A.P.E.L.E                                     | NULL       |      1 | NULL    | NULL    |      0 |   NULL | 2016-10-25 09:27:08 | NULL  |      1 | 2           | NULL             | NULL        | NULL                    | 0                           | 67000 | Adresse ?        |           NULL |       1 | NULL      | NULL | NULL | NULL  | NULL  |        NULL |      NULL |               NULL | NULL        | NULL  | NULL  | NULL | NULL    | NULL    | NULL    | NULL      |    NULL |         0 | NULL         | NULL        | NULL      | NULL        |      1 |           0 | NULL             | NULL             |         NULL | NULL               |            0 |             0 |             0 |             1 |          NULL |             0 |           NULL |           NULL |                    NULL |                    NULL |               NULL |        1 |               0 |            NULL |               0 |            NULL | NULL    |               0 |        NULL |              NULL | fr_FR        | NULL | NULL   | 20161025092708 | NULL            | NULL            |             NULL | NULL               |
|     3 | A.Sauv.Nat. BRUMATH & environs KAPFER Gérard  | NULL       |      1 | NULL    | NULL    |      0 |   NULL | 2016-10-25 09:27:08 | NULL  |      1 | 4           | NULL             | NULL        | NULL                    | 2  rue du Maire  Cornélius  | 67170 | BRUMATH          |           NULL |       1 | NULL      | NULL | NULL | NULL  | NULL  |        NULL |      NULL |               NULL | NULL        | NULL  | NULL  | NULL | NULL    | NULL    | NULL    | NULL      |    NULL |         0 | NULL         | NULL        | NULL      | NULL        |      1 |           0 | NULL             | NULL             |         NULL | NULL               |            0 |             0 |             0 |             1 |          NULL |             0 |           NULL |           NULL |                    NULL |                    NULL |               NULL |         1 |               0 |            NULL |               0 |            NULL | NULL    |               0 |        NULL |              NULL | fr_FR        | NULL | NULL   | 20161025092708 | NULL            | NULL            |             NULL | NULL               |
+-------+-----------------------------------------------+------------+--------+---------+---------+--------+--------+---------------------+-------+--------+-------------+------------------+-------------+-------------------------+-----------------------------+-------+------------------+----------------+---------+-----------+------+------+-------+-------+-------------+-----------+--------------------+-------------+-------+-------+------+---------+---------+---------+-----------+---------+-----------+--------------+-------------+-----------+-------------+--------+-------------+------------------+------------------+--------------+--------------------+--------------+---------------+---------------+---------------+---------------+---------------+----------------+----------------+-------------------------+-------------------------+--------------------+-----------+-----------------+-----------------+-----------------+-----------------+---------+-----------------+-------------+-------------------+--------------+------+--------+----------------+-----------------+-----------------+------------------+--------------------+

要更新第一行,我可以写:

UPDATE `dolibarr`.`llx_societe` SET `rowid` = '1' WHERE `llx_societe`.`rowid` = 5703;

但是我如何为所有行进行此操作?

谢谢!

4 个答案:

答案 0 :(得分:2)

这会吗?

UPDATE `dolibarr`.`llx_societe` SET `rowid` = `rowid` - 5702;

假设id序列中没有间隙。

答案 1 :(得分:2)

从所有行中减去偏移量(5702)。请务必使用ORDER BY子句来防止重复键错误。

UPDATE `dolibarr`.`llx_societe` SET `rowid` = `rowid` - 5702 ORDER BY `rowid`;

答案 2 :(得分:1)

你可以使用一个过程并做一个循环来开始你的行在1并将下一行增加1这样(不知道它是否有效,但这是一个想法):

CREATE PROCEDURE rename()
BEGIN
  DECLARE count,id INT default 0;
  DECLARE cur1 CURSOR FOR SELECT MIN(rowid) FROM dolibarr.llx_societe;
  DECLARE cur2 CURSOR FOR SELECT rowid FROM dolibarr.llx_societe;
  OPEN cur1;
  OPEN cur1;
  set done = 0;
  read_loop: LOOP
    FETCH cur1 INTO count;
    FETCH cur2 INTO id;
    if done = 1 then leave read_loop; end if;
    UPDATE `dolibarr`.`llx_societe` SET @id = @count + 1;
    set count = count+1;
  END LOOP;
  CLOSE cur1;
  CLOSE cur2;
END;

答案 3 :(得分:0)

请注意,如果更改表llx_societe上的id,则将断开指向此表的opther表中的所有链接(例如,表llx_propal中的字段fk_soc,但涉及到很多表)