IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Requêtes MySQL Discussion :

INSERT WHERE.. ON DUPLICATE KEY UPDATE


Sujet :

Requêtes MySQL

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2011
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2011
    Messages : 48
    Points : 21
    Points
    21
    Par défaut INSERT WHERE.. ON DUPLICATE KEY UPDATE
    Bonjour,

    Ci-dessous ma requête :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
     
    CREATE TEMPORARY TABLE TMP 
    (
      active TINYINT,
      ref VARCHAR(255),
    	idfam INT,
    	desi VARCHAR(255),
    	ean DOUBLE,
    	poids DOUBLE,
    	prix DECIMAL(20,3),
    	quantité DOUBLE,
    	minqty INT,
    	idtva INT,
      link_rewrite VARCHAR(255)
    );
     
    LOAD DATA LOCAL INFILE 'C:\\Users\\Gauthier\\Desktop\\maj_produits_from_wave.txt' 
    REPLACE 
    INTO TABLE TMP 
    FIELDS TERMINATED BY ';' 
    LINES TERMINATED BY '\n' -- ou '\r\n' selon l'ordinateur et le programme utilisés pour créer le fichier
    IGNORE 1 LINES (
    	active,
    	Ref,
    	idfam,
    	desi,
    	ean,
    	poids,
    	prix,
    	quantité,
    	minqty,
    	idtva,
      link_rewrite
    );
     
    INSERT INTO pre5181_product (id_category_default, id_tax_rules_group, ean13, minimal_quantity, price, reference, weight, active, date_add, date_upd)
    SELECT t.idfam, t.idtva, t.ean, t.minqty, t.prix, t.ref, t.poids, t.active, NOW(), NOW()
    FROM TMP t
    WHERE t.active = 1
    ON DUPLICATE KEY UPDATE id_category_default = t.idfam, id_tax_rules_group = t.idtva, ean13 = t.ean, minimal_quantity = t.minqty, price = t.prix, reference = t.ref, weight = t.poids, active = t.active, date_upd = NOW()
    En Mysql, je cherche à faire un INSERT depuis un fichier texte avec une condition (WHERE) et un ON DUPLICATE KEY UPDATE mais sans cette condition, c'est à dire mettre à jour tous les champs même ceux qui ne respectent pas le premier WHERE.

    Est-ce possible ?



    Merci d'avance

  2. #2
    Expert éminent sénior Avatar de Artemus24
    Homme Profil pro
    Agent secret au service du président Ulysses S. Grant !
    Inscrit en
    Février 2011
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Agent secret au service du président Ulysses S. Grant !
    Secteur : Finance

    Informations forums :
    Inscription : Février 2011
    Messages : 6 380
    Points : 19 062
    Points
    19 062
    Par défaut
    Salut tibiscuit.

    Citation Envoyé par tibiscuit
    je cherche à faire un INSERT depuis un fichier texte avec une condition (WHERE) et un ON DUPLICATE KEY UPDATE mais sans cette condition, ...
    C'est quoi ce charabia ??? Soit vous utilisez la clause where, soit vous ne l'utilisez pas.
    On ne peut pas mettre à jour une colonne, sans appliquer la clause where, alors que celle-ci est présente dans votre requête.

    Vous avez un problème de compréhension du but final que vous cherchez à faire.

    Citation Envoyé par tibiscuit
    ... c'est à dire mettre à jour tous les champs même ceux qui ne respectent pas le premier WHERE.
    Ne comprenant pas trop ce que vous cherchez à faire, je vais décomposer votre problème.

    1) je reprends votre script et je crée une table "tmp" afin d'y placer le chargement.

    2) j'ai repris la même requête d'insertion que vous avec un jeu d'essai.

    3) j'utilise cette fois-ci, non pas l'insert, mais le replace.
    La différence se porte sur la colonne "create_date" qui cette fois-ci a été écrasé.
    Si vous désirez mettre à jour toutes les colonnes, c'est cette requête que je vous conseille.

    4) Ici, j'utilise le update, mais sur deux tables en jointure.
    Vous remarquez que je n'ai pas précisez la colonne "update_date" et qu'elle a bien été mise à jour.

    Voici l'exécution de mon script :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    --------------
    SET AUTOCOMMIT = 0
    --------------
     
    --------------
    START TRANSACTION
    --------------
     
    --------------
    DROP DATABASE IF EXISTS `base`
    --------------
     
    --------------
    CREATE DATABASE `base`
        DEFAULT CHARACTER SET `latin1`
        DEFAULT COLLATE       `latin1_general_ci`
    --------------
     
    --------------
    DROP TABLE IF EXISTS `tmp`
    --------------
     
    --------------
    CREATE TABLE `tmp`
    ( `idfam`         integer unsigned not null primary key,
      `active`        tinyint          not null,
      `ref`           varchar(255)     not null,
      `desi`          varchar(255)     not null,
      `ean`           decimal(15,3)    not null,
      `poids`         decimal(15,3)    not null,
      `prix`          decimal(15,3)    not null,
      `quantite`      integer unsigned not null,
      `minqty`        integer unsigned not null,
      `idtva`         integer unsigned not null,
      `link_rewrite`  varchar(255)     not null
    ) ENGINE=InnoDB
      DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
      ROW_FORMAT=COMPRESSED
    --------------
     
    --------------
    LOAD DATA LOCAL INFILE 'maj_produits_from_wave.txt'
         INTO TABLE `TMP`
         CHARACTER SET LATIN1
         FIELDS TERMINATED           BY ';'
                OPTIONALLY ENCLOSED  BY '\"'
                ESCAPED              BY '\\'
         LINES  TERMINATED           BY '\r\n'
     
         IGNORE 1 LINES
             (@F01,@F02,@F03,@F04,@F05,@F06,@F07,@F08,@F09,@F10,@F11)
     
         SET `active`       = trim(@F01),
             `ref`          = trim(REPLACE(@F02, '"', '')),
             `idfam`        = trim(@F03),
             `desi`         = trim(REPLACE(@F04, '"', '')),
             `ean`          = trim(@F05),
             `poids`        = trim(@F06),
             `prix`         = trim(@F07),
             `quantite`     = trim(@F08),
             `minqty`       = trim(@F09),
             `idtva`        = trim(@F10),
             `link_rewrite` = trim(REPLACE(@F11, '"', ''))
    --------------
     
    --------------
    select * from tmp
    --------------
     
    +-------+--------+--------+----------------+--------+---------+--------+----------+--------+-------+--------------+
    | idfam | active | ref    | desi           | ean    | poids   | prix   | quantite | minqty | idtva | link_rewrite |
    +-------+--------+--------+----------------+--------+---------+--------+----------+--------+-------+--------------+
    |    10 |      1 | un     | designation 01 | 10.010 | 125.000 | 33.330 |        1 |      1 |    15 | bla bla bla  |
    |    20 |      0 | deux   | designation 02 | 11.020 | 125.000 | 33.330 |        1 |      1 |    15 | bla bla bla  |
    |    30 |      1 | trois  | designation 03 | 12.030 | 125.000 | 33.330 |        1 |      1 |    15 | bla bla bla  |
    |    40 |      0 | quatre | designation 04 | 13.040 | 125.000 | 33.330 |        1 |      1 |    15 | bla bla bla  |
    |    50 |      1 | cinq   | designation 05 | 14.050 | 125.000 | 33.330 |        1 |      1 |    15 | bla bla bla  |
    |    60 |      0 | six    | designation 06 | 15.060 | 125.000 | 33.330 |        1 |      1 |    15 | bla bla bla  |
    |    70 |      1 | sept   | designation 07 | 16.070 | 125.000 | 33.330 |        1 |      1 |    15 | bla bla bla  |
    |    80 |      0 | huit   | designation 08 | 17.080 | 125.000 | 33.330 |        1 |      1 |    15 | bla bla bla  |
    |    90 |      1 | neuf   | designation 09 | 18.090 | 125.000 | 33.330 |        1 |      1 |    15 | bla bla bla  |
    |   100 |      0 | dix    | designation 10 | 19.100 | 125.000 | 33.330 |        1 |      1 |    15 | bla bla bla  |
    |   110 |      1 | onze   | designation 11 | 20.110 | 125.000 | 33.330 |        1 |      1 |    15 | bla bla bla  |
    |   120 |      0 | douze  | designation 12 | 21.120 | 125.000 | 33.330 |        1 |      1 |    15 | bla bla bla  |
    +-------+--------+--------+----------------+--------+---------+--------+----------+--------+-------+--------------+
    --------------
    DROP TABLE IF EXISTS `pre5181_product`
    --------------
     
    --------------
    CREATE TABLE `pre5181_product`
    ( `id_category_default`  integer unsigned not null primary key,
      `id_tax_rules_group`   integer unsigned not null,
      `ean13`                decimal(15,3)    not null,
      `minimal_quantity`     integer unsigned not null,
      `price`                decimal(15,3)    not null,
      `reference`            varchar(255)     not null,
      `weight`               decimal(15,3)    not null,
      `active`               tinyint          not null,
      `create_date`          TIMESTAMP(6)     DEFAULT current_timestamp(6),
      `update_date`          TIMESTAMP(6)     DEFAULT current_timestamp(6) ON UPDATE CURRENT_TIMESTAMP(6)
    ) ENGINE=InnoDB
      DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
      ROW_FORMAT=COMPRESSED
    --------------
     
    --------------
    INSERT INTO `pre5181_product` (`id_category_default`,`id_tax_rules_group`,`ean13`,`minimal_quantity`,`price`,`reference`,`weight`,`active`,`create_date`,`update_date`) values
      (10, 12, 10.01, 2, 120.00, 'seize', 10, 1, '2013-11-10 15:00:00', '2013-11-10 15:00:00'),
      (20, 12, 10.01, 2, 120.00, 'seize', 10, 1, '2013-11-10 15:00:00', '2013-11-10 15:00:00'),
      (30, 12, 10.01, 2, 120.00, 'seize', 10, 1, '2013-11-10 15:00:00', '2013-11-10 15:00:00')
    --------------
     
    --------------
    INSERT INTO `pre5181_product` (`id_category_default`,`id_tax_rules_group`,`ean13`,`minimal_quantity`,`price`,`reference`,`weight`,`active`)
        SELECT  t.idfam  AS id_category_default,
                t.idtva  AS id_tax_rules_group,
                t.ean    AS ean13,
                t.minqty AS minimal_quantity,
                t.prix   AS price,
                t.ref    AS reference,
                t.poids  AS weight,
                t.active AS active
     
          FROM  TMP as t
         WHERE  active = 1
     
    ON DUPLICATE KEY UPDATE id_category_default = t.idfam,
                            id_tax_rules_group  = t.idtva,
                            ean13               = t.ean,
                            minimal_quantity    = t.minqty,
                            price               = t.prix,
                            reference           = t.ref,
                            weight              = t.poids,
                            active              = t.active
    --------------
     
    --------------
    select * from pre5181_product
    --------------
     
    +---------------------+--------------------+--------+------------------+---------+-----------+---------+--------+----------------------------+----------------------------+
    | id_category_default | id_tax_rules_group | ean13  | minimal_quantity | price   | reference | weight  | active | create_date                | update_date                |
    +---------------------+--------------------+--------+------------------+---------+-----------+---------+--------+----------------------------+----------------------------+
    |                  10 |                 15 | 10.010 |                1 |  33.330 | un        | 125.000 |      1 | 2013-11-10 15:00:00.000000 | 2016-11-12 02:54:10.651555 |
    |                  20 |                 12 | 10.010 |                2 | 120.000 | seize     |  10.000 |      1 | 2013-11-10 15:00:00.000000 | 2013-11-10 15:00:00.000000 |
    |                  30 |                 15 | 12.030 |                1 |  33.330 | trois     | 125.000 |      1 | 2013-11-10 15:00:00.000000 | 2016-11-12 02:54:10.651555 |
    |                  50 |                 15 | 14.050 |                1 |  33.330 | cinq      | 125.000 |      1 | 2016-11-12 02:54:10.651555 | 2016-11-12 02:54:10.651555 |
    |                  70 |                 15 | 16.070 |                1 |  33.330 | sept      | 125.000 |      1 | 2016-11-12 02:54:10.651555 | 2016-11-12 02:54:10.651555 |
    |                  90 |                 15 | 18.090 |                1 |  33.330 | neuf      | 125.000 |      1 | 2016-11-12 02:54:10.651555 | 2016-11-12 02:54:10.651555 |
    |                 110 |                 15 | 20.110 |                1 |  33.330 | onze      | 125.000 |      1 | 2016-11-12 02:54:10.651555 | 2016-11-12 02:54:10.651555 |
    +---------------------+--------------------+--------+------------------+---------+-----------+---------+--------+----------------------------+----------------------------+
    --------------
    TRUNCATE TABLE `pre5181_product`
    --------------
     
    --------------
    INSERT INTO `pre5181_product` (`id_category_default`,`id_tax_rules_group`,`ean13`,`minimal_quantity`,`price`,`reference`,`weight`,`active`,`create_date`,`update_date`) values
      (10, 12, 10.01, 2, 120.00, 'seize', 10, 1, '2013-11-10 15:00:00', '2013-11-10 15:00:00'),
      (20, 12, 10.01, 2, 120.00, 'seize', 10, 1, '2013-11-10 15:00:00', '2013-11-10 15:00:00'),
      (30, 12, 10.01, 2, 120.00, 'seize', 10, 1, '2013-11-10 15:00:00', '2013-11-10 15:00:00')
    --------------
     
    --------------
    REPLACE INTO `pre5181_product` (`id_category_default`,`id_tax_rules_group`,`ean13`,`minimal_quantity`,`price`,`reference`,`weight`,`active`)
         SELECT  t.idfam  AS id_category_default,
                 t.idtva  AS id_tax_rules_group,
                 t.ean    AS ean13,
                 t.minqty AS minimal_quantity,
                 t.prix   AS price,
                 t.ref    AS reference,
                 t.poids  AS weight,
                 t.active AS active
     
           FROM  TMP as t
          WHERE  active = 1
    --------------
     
    --------------
    select * from pre5181_product
    --------------
     
    +---------------------+--------------------+--------+------------------+---------+-----------+---------+--------+----------------------------+----------------------------+
    | id_category_default | id_tax_rules_group | ean13  | minimal_quantity | price   | reference | weight  | active | create_date                | update_date                |
    +---------------------+--------------------+--------+------------------+---------+-----------+---------+--------+----------------------------+----------------------------+
    |                  10 |                 15 | 10.010 |                1 |  33.330 | un        | 125.000 |      1 | 2016-11-12 02:54:10.936702 | 2016-11-12 02:54:10.936702 |
    |                  20 |                 12 | 10.010 |                2 | 120.000 | seize     |  10.000 |      1 | 2013-11-10 15:00:00.000000 | 2013-11-10 15:00:00.000000 |
    |                  30 |                 15 | 12.030 |                1 |  33.330 | trois     | 125.000 |      1 | 2016-11-12 02:54:10.936702 | 2016-11-12 02:54:10.936702 |
    |                  50 |                 15 | 14.050 |                1 |  33.330 | cinq      | 125.000 |      1 | 2016-11-12 02:54:10.936702 | 2016-11-12 02:54:10.936702 |
    |                  70 |                 15 | 16.070 |                1 |  33.330 | sept      | 125.000 |      1 | 2016-11-12 02:54:10.936702 | 2016-11-12 02:54:10.936702 |
    |                  90 |                 15 | 18.090 |                1 |  33.330 | neuf      | 125.000 |      1 | 2016-11-12 02:54:10.936702 | 2016-11-12 02:54:10.936702 |
    |                 110 |                 15 | 20.110 |                1 |  33.330 | onze      | 125.000 |      1 | 2016-11-12 02:54:10.936702 | 2016-11-12 02:54:10.936702 |
    +---------------------+--------------------+--------+------------------+---------+-----------+---------+--------+----------------------------+----------------------------+
    --------------
    TRUNCATE TABLE `pre5181_product`
    --------------
     
    --------------
    INSERT INTO `pre5181_product` (`id_category_default`,`id_tax_rules_group`,`ean13`,`minimal_quantity`,`price`,`reference`,`weight`,`active`,`create_date`,`update_date`) values
      (10, 12, 10.01, 2, 120.00, 'seize', 10, 1, '2013-11-10 15:00:00', '2013-11-10 15:00:00'),
      (20, 12, 10.01, 2, 120.00, 'seize', 10, 1, '2013-11-10 15:00:00', '2013-11-10 15:00:00'),
      (30, 12, 10.01, 2, 120.00, 'seize', 10, 1, '2013-11-10 15:00:00', '2013-11-10 15:00:00')
    --------------
     
    --------------
    UPDATE      `pre5181_product` as t1
    INNER JOIN  `tmp`             as t2
            ON  t2.idfam               = t1.id_category_default
           SET  t1.id_tax_rules_group  = t2.idtva,
                t1.ean13               = t2.ean,
                t1.minimal_quantity    = t2.minqty,
                t1.price               = t2.prix,
                t1.reference           = t2.ref,
                t1.weight              = t2.poids,
                t1.active              = t2.active
    --------------
     
    --------------
    select * from pre5181_product
    --------------
     
    +---------------------+--------------------+--------+------------------+--------+-----------+---------+--------+----------------------------+----------------------------+
    | id_category_default | id_tax_rules_group | ean13  | minimal_quantity | price  | reference | weight  | active | create_date                | update_date                |
    +---------------------+--------------------+--------+------------------+--------+-----------+---------+--------+----------------------------+----------------------------+
    |                  10 |                 15 | 10.010 |                1 | 33.330 | un        | 125.000 |      1 | 2013-11-10 15:00:00.000000 | 2016-11-12 02:54:11.235245 |
    |                  20 |                 15 | 11.020 |                1 | 33.330 | deux      | 125.000 |      0 | 2013-11-10 15:00:00.000000 | 2016-11-12 02:54:11.235245 |
    |                  30 |                 15 | 12.030 |                1 | 33.330 | trois     | 125.000 |      1 | 2013-11-10 15:00:00.000000 | 2016-11-12 02:54:11.235245 |
    +---------------------+--------------------+--------+------------------+--------+-----------+---------+--------+----------------------------+----------------------------+
    --------------
    COMMIT
    --------------
     
    --------------
    SET AUTOCOMMIT = 1
    --------------
     
    Appuyez sur une touche pour continuer...
    Citation Envoyé par tibiscuit
    Est-ce possible ?
    Vu que je n'ai pas bien compris ce que vous essayez de faire, je ne peux pas répondre explicitement à cette question.

    Le mieux est de reformuler correctement votre demande afin d'être mieux compris.

    Edit: comme il manquait un bout de mon code, j'ai modifié ce message en replaçant le bonne exécution.
    @+
    Si vous êtes de mon aide, vous pouvez cliquer sur .
    Mon site : http://www.jcz.fr

Discussions similaires

  1. INSERT ON DUPLICATE KEY UPDATE
    Par karl2mil1 dans le forum Requêtes
    Réponses: 1
    Dernier message: 21/01/2011, 15h47
  2. Insert, on duplicate key update
    Par knolz dans le forum MS SQL Server
    Réponses: 4
    Dernier message: 30/06/2010, 08h33
  3. INSERT .. ON DUPLICATE KEY UPDATE fait que des INSERT !
    Par umeboshi dans le forum Requêtes
    Réponses: 2
    Dernier message: 30/07/2008, 09h40
  4. INSERT ... ON DUPLICATE KEY UPDATE
    Par luffy san dans le forum PostgreSQL
    Réponses: 2
    Dernier message: 17/10/2005, 17h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo