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 :

update pour combler des trous


Sujet :

Requêtes MySQL

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2004
    Messages
    225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2004
    Messages : 225
    Points : 97
    Points
    97
    Par défaut update pour combler des trous
    Bonjour,

    un update est-il possible pour remplacer les zéro dans les intervalles de valeur des id ?

    tirage id

    T01 108
    T02 0
    T03 0
    T04 0
    T05 0
    T06 0
    T07 0
    T08 0
    T09 0
    T10 0
    T01 109
    T02 0
    T03 0
    T04 0
    T05 0
    T06 0
    T07 0
    T08 0
    T09 0
    T10 0
    T01 110
    T02 0
    T03 0
    T04 0
    T05 0
    T06 0
    T07 0
    T08 0
    T09 0
    T10 0
    ....


    Pour obtenir ceci :

    tirage id

    T01 108
    T02 108
    T03 108
    T04 108
    T05 108
    T06 108
    T07 108
    T08 108
    T09 108
    T10 108

    T01 109
    T02 109
    T03 109
    T04 109
    T05 109
    T06 109
    T07 109
    T08 109
    T09 109
    T10 109

    T01 110
    T02 110
    .....

    Merci d'avance pour toute aide,
    n

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2004
    Messages
    225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2004
    Messages : 225
    Points : 97
    Points
    97
    Par défaut
    en fait la table est plus complète que ça , voici un extrait:

    # tirage_id, tirage_libelle, tirage_heure_debut, tirage_heure_fin, tirage_comment, tirage_id_campagne, tirage_id_tmp, campagne_id_tmp
    '23516', 'Tirage T01', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '', '108', '0', '8252'
    '23517', 'Tirage T02', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '', '0', '0', '8255'
    '23518', 'Tirage T03', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '', '0', '0', '8258'
    '23519', 'Tirage T04', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '', '0', '0', '8261'
    '23520', 'Tirage T05', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '', '0', '0', '8264'
    '23521', 'Tirage T06', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '', '0', '0', '8267'
    '23522', 'Tirage T07', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '', '0', '0', '8270'
    '23523', 'Tirage T08', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '', '0', '0', '8273'
    '23524', 'Tirage T09', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '', '0', '0', '8276'
    '23525', 'Tirage T10', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '', '0', '0', '8279'
    '23526', 'Tirage T01', '2014-05-16 13:30:00', '2014-05-16 14:00:00', '', '109', '0', '8462'
    '23527', 'Tirage T02', '2014-05-16 16:45:00', '2014-05-16 17:00:00', '', '0', '0', '8465'
    '23528', 'Tirage T03', '2014-05-16 14:00:00', '2014-05-16 14:30:00', '', '0', '0', '8468'
    '23529', 'Tirage T04', '2014-05-16 15:54:00', '2014-05-16 16:05:00', '', '0', '0', '8471'
    '23530', 'Tirage T05', '2014-05-16 14:30:00', '2014-05-16 14:40:00', '', '0', '0', '8474'
    '23531', 'Tirage T06', '2014-05-16 14:43:00', '2014-05-16 14:54:00', '', '0', '0', '8477'
    '23532', 'Tirage T07', '2014-05-16 14:56:00', '2014-05-16 15:08:00', '', '0', '0', '8480'
    '23533', 'Tirage T08', '2014-05-16 15:15:00', '2014-05-16 15:27:00', '', '0', '0', '8483'
    '23534', 'Tirage T09', '2014-05-16 15:32:00', '2014-05-16 15:42:00', '', '0', '0', '8486'
    '23535', 'Tirage T10', '2014-05-16 16:20:00', '2014-05-16 16:35:00', '', '0', '0', '8489'
    '23536', 'Tirage T01', '2014-06-05 14:40:00', '2014-06-05 14:48:00', '', '110', '0', '8222'
    '23537', 'Tirage T02', '2014-06-05 14:50:00', '2014-06-05 15:10:00', '', '0', '0', '8225'
    '23538', 'Tirage T03', '2014-06-05 14:59:00', '2014-06-05 15:13:00', '', '0', '0', '8228'
    '23539', 'Tirage T04', '2014-06-05 17:26:00', '2014-06-05 18:00:00', '', '0', '0', '8231'
    '23540', 'Tirage T05', '2014-06-05 17:08:00', '2014-06-05 17:20:00', '', '0', '0', '8234'
    '23541', 'Tirage T06', '2014-06-05 15:25:00', '2014-06-05 15:50:00', '', '0', '0', '8237'
    '23542', 'Tirage T07', '2014-06-05 15:27:00', '2014-06-05 15:45:00', '', '0', '0', '8240'
    '23543', 'Tirage T08', '2014-06-05 16:05:00', '2014-06-05 16:25:00', '', '0', '0', '8243'
    '23544', 'Tirage T09', '2014-06-05 16:35:00', '2014-06-05 16:58:00', '', '0', '0', '8246'
    '23545', 'Tirage T10', '2014-06-05 17:25:00', '2014-06-05 18:00:00', '', '0', '0', '8249'
    '23546', 'Tirage T01', '2014-07-17 09:00:00', '2014-07-17 09:16:00', '', '111', '0', '8522'
    '23547', 'Tirage T02', '2014-07-17 09:15:00', '2014-07-17 09:25:00', '', '0', '0', '8525'

    je sais pas si ça peut aider ?
    merci d'avance !

  3. #3
    Modérateur
    Avatar de al1_24
    Homme Profil pro
    Retraité
    Inscrit en
    Mai 2002
    Messages
    9 080
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 63
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Retraité
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2002
    Messages : 9 080
    Points : 30 803
    Points
    30 803
    Par défaut
    Citation Envoyé par nine Voir le message
    je sais pas si ça peut aider ?
    En effet, ça peut aider ; l'ordre des lignes est important pour identifier la valeur à prendre en compte et la colonne tirage_id est ici bien utile.

    Quelque chose comme ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    update   ma_table tbl
    set      tbl.tirage_id_campagne
                =  (  select   max(sel.tirage_id_campagne)
                      from     ma_table sel
                      where    sel.tirage_id < tbl.tirage_id
                   )
    where    tbl.tirage_id_campagne = 0
    Modérateur Langage SQL
    Règles du forum Langage SQL à lire par tous, N'hésitez pas à consulter les cours SQL
    N'oubliez pas le bouton et pensez aux balises
    [code]
    Si une réponse vous a aidé à résoudre votre problème, n'oubliez pas de voter pour elle en cliquant sur
    Aide-toi et le forum t'aidera : Un problème exposé sans mentionner les tentatives de résolution infructueuses peut laisser supposer que le posteur attend qu'on fasse son travail à sa place... et ne donne pas envie d'y répondre.

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2004
    Messages
    225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2004
    Messages : 225
    Points : 97
    Points
    97
    Par défaut
    merci !

    lorsque j'execute la requete j'ai l'erreur suivante :

    12:03:01 update cceid_tirage tbl set tbl.tirage_id_campagne = ( select max(sel.tirage_id_campagne) from cceid_tirage sel where sel.tirage_id < tbl.tirage_id ) where tbl.tirage_id_campagne = 0


    Error Code: 1093. You can't specify target table 'tbl' for update in FROM clause 0.00087 sec

  5. #5
    Modérateur
    Avatar de al1_24
    Homme Profil pro
    Retraité
    Inscrit en
    Mai 2002
    Messages
    9 080
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 63
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Retraité
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2002
    Messages : 9 080
    Points : 30 803
    Points
    30 803
    Par défaut
    C'est en effet une des limitations de MySQL (et la preuve que ce n'est pas vraiment un SGBD) à laquelle je n'avais pas pensé.
    Modérateur Langage SQL
    Règles du forum Langage SQL à lire par tous, N'hésitez pas à consulter les cours SQL
    N'oubliez pas le bouton et pensez aux balises
    [code]
    Si une réponse vous a aidé à résoudre votre problème, n'oubliez pas de voter pour elle en cliquant sur
    Aide-toi et le forum t'aidera : Un problème exposé sans mentionner les tentatives de résolution infructueuses peut laisser supposer que le posteur attend qu'on fasse son travail à sa place... et ne donne pas envie d'y répondre.

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2004
    Messages
    225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2004
    Messages : 225
    Points : 97
    Points
    97
    Par défaut
    zut ... j'ai essayé comme ceci :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    update   cceid_tirage tbl
    set      tbl.tirage_id_campagne
                =  (  select   max(sel.tirage_id_campagne)
                      from     cceid_tirage sel inner join cceid_tirage t3 on sel.tirage_id = t3.tirage_id 
                      where    sel.tirage_id < t3.tirage_id
                   )
    where    tbl.tirage_id_campagne = 0;

    mais j'ai le même message alors que tbl n'apparait plus ??!!

  7. #7
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2004
    Messages
    225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2004
    Messages : 225
    Points : 97
    Points
    97
    Par défaut
    bon, je n'ai plus d'erreur mais ça ne fonctionne pas avec :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    update 
    	cceid_tirage as t1 
    	inner join (
    		select max(sel.tirage_id_campagne) as tirageIDCMP, sel.tirage_id as tirageID
                      from     cceid_tirage sel 
                      where sel.campagne_id_tmp > 0 
                      and sel.tirage_id_campagne > 0
    				  group by sel.tirage_id
            ) as t4 on t1.tirage_id = t4.tirageID
    		set t1.tirage_id_campagne = t4.tirageIDCMP;


    une idée ? merci

  8. #8
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2004
    Messages
    225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2004
    Messages : 225
    Points : 97
    Points
    97
    Par défaut
    bonjour


    j'ai essaye ceci:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
     
    update cceid_tirage t1
    inner join cceid_tirage t2
    	on t1.tirage_id = t2.tirage_id
                 set t1.tirage_id_campagne = t2.tirage_id_campagne 
    		  where  (t2.tirage_id + 1) - t1.tirage_id <10  ;
    je match bien toutes les lignes sans pourtant en changer une seule .... je sens que je ne suis plus très loin !

  9. #9
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2004
    Messages
    225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2004
    Messages : 225
    Points : 97
    Points
    97
    Par défaut
    bonjour,


    je tente une autre approche en passant par une table intermédiaire, dans laquelle je stock ce dont j'ai besoin , voici un aperçu :

    la requete est :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
     
    select t1.tirage_id,tmp.tirageIDCMP, tmp.id1, tmp.id2, tmp.id3, tmp.id4, tmp.id5, tmp.id6, tmp.id7, tmp.id8, tmp.id9 from cceid_tirage t1 
    	left join tirage_tmp tmp
    		on t1.tirage_id = tmp.tirageID 
    			where t1.campagne_id_tmp >0
    					order by t1.tirage_id ;
    le resultat :

    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
     
     
    # tirage_id, tirageIDCMP, id1, id2, id3, id4, id5, id6, id7, id8, id9
    23516, 108, 23517, 23518, 23519, 23520, 23521, 23522, 23523, 23524, 23525
    23517, , , , , , , , , , 
    23518, , , , , , , , , , 
    23519, , , , , , , , , , 
    23520, , , , , , , , , , 
    23521, , , , , , , , , , 
    23522, , , , , , , , , , 
    23523, , , , , , , , , , 
    23524, , , , , , , , , , 
    23525, , , , , , , , , , 
    23526, 109, 23527, 23528, 23529, 23530, 23531, 23532, 23533, 23534, 23535
    23527, , , , , , , , , , 
    23528, , , , , , , , , , 
    23529, , , , , , , , , , 
    23530, , , , , , , , , , 
    23531, , , , , , , , , , 
    23532, , , , , , , , , , 
    23533, , , , , , , , , , 
    23534, , , , , , , , , , 
    23535, , , , , , , , , , 
    23536, 110, 23537, 23538, 23539, 23540, 23541, 23542, 23543, 23544, 23545
    ce que je souhaite faire c'est :
    pour chaque tirage_id (23516 par ex), j'update le champs tirage_id_campagne avec tirageIDCMP (108) pour tirage_id in (id1 à id9).

    pour rappel ma " vrai " table ressemble a ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    # tirage_id, tirage_libelle, tirage_heure_debut, tirage_heure_fin, tirage_comment, tirage_id_campagne, tirage_id_tmp, campagne_id_tmp
    23516, Tirage T01, 2014-04-07 12:00:00, 2014-04-07 13:00:00, , 108, 0, 8252
    23517, Tirage T02, 2014-04-07 12:00:00, 2014-04-07 13:00:00, , 0, 0, 8255
    23518, Tirage T03, 2014-04-07 12:00:00, 2014-04-07 13:00:00, , 0, 0, 8258
    23519, Tirage T04, 2014-04-07 12:00:00, 2014-04-07 13:00:00, , 0, 0, 8261
    23520, Tirage T05, 2014-04-07 12:00:00, 2014-04-07 13:00:00, , 0, 0, 8264
    23521, Tirage T06, 2014-04-07 12:00:00, 2014-04-07 13:00:00, , 0, 0, 8267
    23522, Tirage T07, 2014-04-07 12:00:00, 2014-04-07 13:00:00, , 0, 0, 8270
    23523, Tirage T08, 2014-04-07 12:00:00, 2014-04-07 13:00:00, , 0, 0, 8273
    23524, Tirage T09, 2014-04-07 12:00:00, 2014-04-07 13:00:00, , 0, 0, 8276
    23525, Tirage T10, 2014-04-07 12:00:00, 2014-04-07 13:00:00, , 0, 0, 8279
    23526, Tirage T01, 2014-05-16 13:30:00, 2014-05-16 14:00:00, , 109, 0, 8462
    23527, Tirage T02, 2014-05-16 16:45:00, 2014-05-16 17:00:00, , 0, 0, 8465
    et voici enfin la requete que je tente pour mettre à jour ma table :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    update cceid_tirage left join 
    tirage_tmp tmp on tirage_id = tmp.tirageID
     set tirage_id_campagne = tmp.tirageIDCMP 
    	where tirage_id in (tmp.id1, tmp.id2, tmp.id3, tmp.id4, tmp.id5, tmp.id6, tmp.id7, tmp.id8, tmp.id9);
    mais ça ne match pas , là je ne comprends vraiment pas, help !

    merci d'avance

  10. #10
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2004
    Messages
    225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2004
    Messages : 225
    Points : 97
    Points
    97
    Par défaut
    j'ai essayé ceci :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
     
    update cceid_tirage t1
          left join 
    		( 
                select @idCMP := previous.tirageIDCMP as tirageIDCMP, previous.tirageID as tirageID 
                from tirage_tmp previous inner join cceid_tirage current
    			on previous.tirageID = current.tirage_id 
    			where current.campagne_id_tmp >0 -- and previous.tirage_id_campagne <>0
              ) as t4  on t1.tirage_id = t4.tirageID
              set t1.tirage_id_campagne = @idCMP;
    alors, la bonne nouvelle : pour la première fois ça a fait l'update !
    la mauvaise :
    c'est pas les bonnes valeurs : j'ai la dernière valeur (298 ) inscrite partout !!

    mais je penssais que la variable @idCMP se mettait à jour à chaque tuple matché ? je sais pas si je suis clair ....

  11. #11
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2004
    Messages
    225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2004
    Messages : 225
    Points : 97
    Points
    97
    Par défaut
    quand on lâche rien ... on obtient !!!!

    ça y est , la bonne requête eest :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
     
    update cceid_tirage 
    	set tirage_id_campagne = (select tirageIDCMP from tirage_tmp where cceid_tirage.tirage_id in(  id1 ,id2,id3,id4,id5,id6,id7,id8,id9) ) 
            where tirage_id_campagne =0;
    tout simplement !

    il ne fallait pas faire de jointure puisque on avait accès aux id à updater directement dans les colonnes id1 à id9 .... voilà !

    bonne continuation à tous !

  12. #12
    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 381
    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 381
    Points : 19 065
    Points
    19 065
    Par défaut
    Salut nine.

    Voici trois exemples :
    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
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    --------------
    START TRANSACTION
    --------------
     
    --------------
    DROP DATABASE IF EXISTS `base`
    --------------
     
    --------------
    CREATE DATABASE IF NOT EXISTS `base`
            DEFAULT CHARACTER SET `latin1`
            DEFAULT COLLATE       `latin1_general_ci`
    --------------
     
    --------------
    DROP TABLE IF EXISTS `test`
    --------------
     
    --------------
    CREATE TABLE `test`
    ( `tirage_id`          integer unsigned  NOT NULL auto_increment primary key,
      `tirage_libelle`     varchar(255)      NOT NULL,
      `tirage_heure_debut` datetime          NOT NULL,
      `tirage_heure_fin`   datetime          NOT NULL,
      `tirage_comment`     varchar(255)      NOT NULL,
      `tirage_id_campagne` integer unsigned  NOT NULL,
      `tirage_id_tmp`      integer unsigned  NOT NULL,
      `campagne_id_tmp`    integer unsigned  NOT NULL
    ) ENGINE=InnoDB
      DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
      ROW_FORMAT=COMPRESSED
    --------------
     
    --------------
    insert into `test` (`tirage_id`,`tirage_libelle`,`tirage_heure_debut`,`tirage_heure_fin`,`tirage_comment`,`tirage_id_campagne`,`tirage_id_tmp`,`campagne_id_tmp`) values
      (23516, 'Tirage T01', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '', 108, 0, 8252),
      (23517, 'Tirage T02', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '',   0, 0, 8255),
      (23518, 'Tirage T03', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '',   0, 0, 8258),
      (23519, 'Tirage T04', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '',   0, 0, 8261),
      (23520, 'Tirage T05', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '',   0, 0, 8264),
      (23521, 'Tirage T06', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '',   0, 0, 8267),
      (23522, 'Tirage T07', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '',   0, 0, 8270),
      (23523, 'Tirage T08', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '',   0, 0, 8273),
      (23524, 'Tirage T09', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '',   0, 0, 8276),
      (23525, 'Tirage T10', '2014-04-07 12:00:00', '2014-04-07 13:00:00', '',   0, 0, 8279),
      (23526, 'Tirage T01', '2014-05-16 13:30:00', '2014-05-16 14:00:00', '', 109, 0, 8462),
      (23527, 'Tirage T02', '2014-05-16 16:45:00', '2014-05-16 17:00:00', '',   0, 0, 8465),
      (23528, 'Tirage T03', '2014-05-16 14:00:00', '2014-05-16 14:30:00', '',   0, 0, 8468),
      (23529, 'Tirage T04', '2014-05-16 15:54:00', '2014-05-16 16:05:00', '',   0, 0, 8471),
      (23530, 'Tirage T05', '2014-05-16 14:30:00', '2014-05-16 14:40:00', '',   0, 0, 8474),
      (23531, 'Tirage T06', '2014-05-16 14:43:00', '2014-05-16 14:54:00', '',   0, 0, 8477),
      (23532, 'Tirage T07', '2014-05-16 14:56:00', '2014-05-16 15:08:00', '',   0, 0, 8480),
      (23533, 'Tirage T08', '2014-05-16 15:15:00', '2014-05-16 15:27:00', '',   0, 0, 8483),
      (23534, 'Tirage T09', '2014-05-16 15:32:00', '2014-05-16 15:42:00', '',   0, 0, 8486),
      (23535, 'Tirage T10', '2014-05-16 16:20:00', '2014-05-16 16:35:00', '',   0, 0, 8489),
      (23536, 'Tirage T01', '2014-06-05 14:40:00', '2014-06-05 14:48:00', '', 110, 0, 8222),
      (23537, 'Tirage T02', '2014-06-05 14:50:00', '2014-06-05 15:10:00', '',   0, 0, 8225),
      (23538, 'Tirage T03', '2014-06-05 14:59:00', '2014-06-05 15:13:00', '',   0, 0, 8228),
      (23539, 'Tirage T04', '2014-06-05 17:26:00', '2014-06-05 18:00:00', '',   0, 0, 8231),
      (23540, 'Tirage T05', '2014-06-05 17:08:00', '2014-06-05 17:20:00', '',   0, 0, 8234),
      (23541, 'Tirage T06', '2014-06-05 15:25:00', '2014-06-05 15:50:00', '',   0, 0, 8237),
      (23542, 'Tirage T07', '2014-06-05 15:27:00', '2014-06-05 15:45:00', '',   0, 0, 8240),
      (23543, 'Tirage T08', '2014-06-05 16:05:00', '2014-06-05 16:25:00', '',   0, 0, 8243),
      (23544, 'Tirage T09', '2014-06-05 16:35:00', '2014-06-05 16:58:00', '',   0, 0, 8246),
      (23545, 'Tirage T10', '2014-06-05 17:25:00', '2014-06-05 18:00:00', '',   0, 0, 8249),
      (23546, 'Tirage T01', '2014-07-17 09:00:00', '2014-07-17 09:16:00', '', 111, 0, 8522),
      (23547, 'Tirage T02', '2014-07-17 09:15:00', '2014-07-17 09:25:00', '',   0, 0, 8525)
    --------------
     
    --------------
    select * from `test`
    --------------
     
    +-----------+----------------+---------------------+---------------------+----------------+--------------------+---------------+-----------------+
    | tirage_id | tirage_libelle | tirage_heure_debut  | tirage_heure_fin    | tirage_comment | tirage_id_campagne | tirage_id_tmp | campagne_id_tmp |
    +-----------+----------------+---------------------+---------------------+----------------+--------------------+---------------+-----------------+
    |     23516 | Tirage T01     | 2014-04-07 12:00:00 | 2014-04-07 13:00:00 |                |                108 |             0 |            8252 |
    |     23517 | Tirage T02     | 2014-04-07 12:00:00 | 2014-04-07 13:00:00 |                |                  0 |             0 |            8255 |
    |     23518 | Tirage T03     | 2014-04-07 12:00:00 | 2014-04-07 13:00:00 |                |                  0 |             0 |            8258 |
    |     23519 | Tirage T04     | 2014-04-07 12:00:00 | 2014-04-07 13:00:00 |                |                  0 |             0 |            8261 |
    |     23520 | Tirage T05     | 2014-04-07 12:00:00 | 2014-04-07 13:00:00 |                |                  0 |             0 |            8264 |
    |     23521 | Tirage T06     | 2014-04-07 12:00:00 | 2014-04-07 13:00:00 |                |                  0 |             0 |            8267 |
    |     23522 | Tirage T07     | 2014-04-07 12:00:00 | 2014-04-07 13:00:00 |                |                  0 |             0 |            8270 |
    |     23523 | Tirage T08     | 2014-04-07 12:00:00 | 2014-04-07 13:00:00 |                |                  0 |             0 |            8273 |
    |     23524 | Tirage T09     | 2014-04-07 12:00:00 | 2014-04-07 13:00:00 |                |                  0 |             0 |            8276 |
    |     23525 | Tirage T10     | 2014-04-07 12:00:00 | 2014-04-07 13:00:00 |                |                  0 |             0 |            8279 |
    |     23526 | Tirage T01     | 2014-05-16 13:30:00 | 2014-05-16 14:00:00 |                |                109 |             0 |            8462 |
    |     23527 | Tirage T02     | 2014-05-16 16:45:00 | 2014-05-16 17:00:00 |                |                  0 |             0 |            8465 |
    |     23528 | Tirage T03     | 2014-05-16 14:00:00 | 2014-05-16 14:30:00 |                |                  0 |             0 |            8468 |
    |     23529 | Tirage T04     | 2014-05-16 15:54:00 | 2014-05-16 16:05:00 |                |                  0 |             0 |            8471 |
    |     23530 | Tirage T05     | 2014-05-16 14:30:00 | 2014-05-16 14:40:00 |                |                  0 |             0 |            8474 |
    |     23531 | Tirage T06     | 2014-05-16 14:43:00 | 2014-05-16 14:54:00 |                |                  0 |             0 |            8477 |
    |     23532 | Tirage T07     | 2014-05-16 14:56:00 | 2014-05-16 15:08:00 |                |                  0 |             0 |            8480 |
    |     23533 | Tirage T08     | 2014-05-16 15:15:00 | 2014-05-16 15:27:00 |                |                  0 |             0 |            8483 |
    |     23534 | Tirage T09     | 2014-05-16 15:32:00 | 2014-05-16 15:42:00 |                |                  0 |             0 |            8486 |
    |     23535 | Tirage T10     | 2014-05-16 16:20:00 | 2014-05-16 16:35:00 |                |                  0 |             0 |            8489 |
    |     23536 | Tirage T01     | 2014-06-05 14:40:00 | 2014-06-05 14:48:00 |                |                110 |             0 |            8222 |
    |     23537 | Tirage T02     | 2014-06-05 14:50:00 | 2014-06-05 15:10:00 |                |                  0 |             0 |            8225 |
    |     23538 | Tirage T03     | 2014-06-05 14:59:00 | 2014-06-05 15:13:00 |                |                  0 |             0 |            8228 |
    |     23539 | Tirage T04     | 2014-06-05 17:26:00 | 2014-06-05 18:00:00 |                |                  0 |             0 |            8231 |
    |     23540 | Tirage T05     | 2014-06-05 17:08:00 | 2014-06-05 17:20:00 |                |                  0 |             0 |            8234 |
    |     23541 | Tirage T06     | 2014-06-05 15:25:00 | 2014-06-05 15:50:00 |                |                  0 |             0 |            8237 |
    |     23542 | Tirage T07     | 2014-06-05 15:27:00 | 2014-06-05 15:45:00 |                |                  0 |             0 |            8240 |
    |     23543 | Tirage T08     | 2014-06-05 16:05:00 | 2014-06-05 16:25:00 |                |                  0 |             0 |            8243 |
    |     23544 | Tirage T09     | 2014-06-05 16:35:00 | 2014-06-05 16:58:00 |                |                  0 |             0 |            8246 |
    |     23545 | Tirage T10     | 2014-06-05 17:25:00 | 2014-06-05 18:00:00 |                |                  0 |             0 |            8249 |
    |     23546 | Tirage T01     | 2014-07-17 09:00:00 | 2014-07-17 09:16:00 |                |                111 |             0 |            8522 |
    |     23547 | Tirage T02     | 2014-07-17 09:15:00 | 2014-07-17 09:25:00 |                |                  0 |             0 |            8525 |
    +-----------+----------------+---------------------+---------------------+----------------+--------------------+---------------+-----------------+
    --------------
    commit
    --------------
     
    --------------
    update      `test` as t1
    inner join  (     select  tirage_id,
                              @val:= if (tirage_id_campagne <> 0, tirage_id_campagne, @val) as val
                        from  `test`
                  cross join  (  SELECT @val:=''  ) as x
                    order by  tirage_id
                ) as t2
            on  t2.tirage_id = t1.tirage_id
     
           set  t1.tirage_id_campagne = t2.val
         where  t1.tirage_id_campagne = 0
    --------------
     
    --------------
    select  tirage_id,
            tirage_id_campagne
     from  `test`
    --------------
     
    +-----------+--------------------+
    | tirage_id | tirage_id_campagne |
    +-----------+--------------------+
    |     23516 |                108 |
    |     23517 |                108 |
    |     23518 |                108 |
    |     23519 |                108 |
    |     23520 |                108 |
    |     23521 |                108 |
    |     23522 |                108 |
    |     23523 |                108 |
    |     23524 |                108 |
    |     23525 |                108 |
    |     23526 |                109 |
    |     23527 |                109 |
    |     23528 |                109 |
    |     23529 |                109 |
    |     23530 |                109 |
    |     23531 |                109 |
    |     23532 |                109 |
    |     23533 |                109 |
    |     23534 |                109 |
    |     23535 |                109 |
    |     23536 |                110 |
    |     23537 |                110 |
    |     23538 |                110 |
    |     23539 |                110 |
    |     23540 |                110 |
    |     23541 |                110 |
    |     23542 |                110 |
    |     23543 |                110 |
    |     23544 |                110 |
    |     23545 |                110 |
    |     23546 |                111 |
    |     23547 |                111 |
    +-----------+--------------------+
    --------------
    rollback
    --------------
     
    --------------
    update      `test` as t1
    inner join  (    select  t3.tirage_id,
                             max(t4.tirage_id_campagne) as tirage_id_campagne
     
                       from  `test` as t3
                 inner join  `test` as t4
                         on  t4.tirage_id <= t3.tirage_id
                   group by  t3.tirage_id
                ) as  t2
                  on  t2.tirage_id = t1.tirage_id
     
                 set  t1.tirage_id_campagne = t2.tirage_id_campagne
               where  t1.tirage_id_campagne = 0
    --------------
     
    --------------
    select  tirage_id,
            tirage_id_campagne
     from  `test`
    --------------
     
    +-----------+--------------------+
    | tirage_id | tirage_id_campagne |
    +-----------+--------------------+
    |     23516 |                108 |
    |     23517 |                108 |
    |     23518 |                108 |
    |     23519 |                108 |
    |     23520 |                108 |
    |     23521 |                108 |
    |     23522 |                108 |
    |     23523 |                108 |
    |     23524 |                108 |
    |     23525 |                108 |
    |     23526 |                109 |
    |     23527 |                109 |
    |     23528 |                109 |
    |     23529 |                109 |
    |     23530 |                109 |
    |     23531 |                109 |
    |     23532 |                109 |
    |     23533 |                109 |
    |     23534 |                109 |
    |     23535 |                109 |
    |     23536 |                110 |
    |     23537 |                110 |
    |     23538 |                110 |
    |     23539 |                110 |
    |     23540 |                110 |
    |     23541 |                110 |
    |     23542 |                110 |
    |     23543 |                110 |
    |     23544 |                110 |
    |     23545 |                110 |
    |     23546 |                111 |
    |     23547 |                111 |
    +-----------+--------------------+
    --------------
    rollback
    --------------
     
    --------------
    update           `test` as t1
    left outer join  `test` as t2
                 on  date(t2.tirage_heure_debut) = date(t1.tirage_heure_debut)
                and       t2.tirage_id_campagne <> 0
     
                set  t1.tirage_id_campagne = t2.tirage_id_campagne
              where  t1.tirage_id_campagne = 0
    --------------
     
    --------------
    select  tirage_id,
            tirage_id_campagne
     from  `test`
    --------------
     
    +-----------+--------------------+
    | tirage_id | tirage_id_campagne |
    +-----------+--------------------+
    |     23516 |                108 |
    |     23517 |                108 |
    |     23518 |                108 |
    |     23519 |                108 |
    |     23520 |                108 |
    |     23521 |                108 |
    |     23522 |                108 |
    |     23523 |                108 |
    |     23524 |                108 |
    |     23525 |                108 |
    |     23526 |                109 |
    |     23527 |                109 |
    |     23528 |                109 |
    |     23529 |                109 |
    |     23530 |                109 |
    |     23531 |                109 |
    |     23532 |                109 |
    |     23533 |                109 |
    |     23534 |                109 |
    |     23535 |                109 |
    |     23536 |                110 |
    |     23537 |                110 |
    |     23538 |                110 |
    |     23539 |                110 |
    |     23540 |                110 |
    |     23541 |                110 |
    |     23542 |                110 |
    |     23543 |                110 |
    |     23544 |                110 |
    |     23545 |                110 |
    |     23546 |                111 |
    |     23547 |                111 |
    +-----------+--------------------+
    --------------
    COMMIT
    --------------
     
    --------------
    SET AUTOCOMMIT = 1
    --------------
     
    Appuyez sur une touche pour continuer...
    La troisième solution étant la meilleure des trois.
    J'ai dû utiliser un critère de sélection, celui de la date qui est la même dans votre bloc allant de t1 à t10.
    Pour une question de performance, cette date aura dû être une colonne à part entière.
    Comme il n'y a qu'une seule valeur de renseignée pour la colonne "tirage_id_campagne", c'est celle-ci que je récupère.
    Et je la propage sur la table t1 qui aura été mise à jour.

    @+
    Si vous êtes de mon aide, vous pouvez cliquer sur .
    Mon site : http://www.jcz.fr

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 12/04/2016, 09h31
  2. Combler des trous avec la valeur précédente
    Par sophie2010 dans le forum Langage SQL
    Réponses: 3
    Dernier message: 12/10/2015, 13h31
  3. Requete pour trouver des trous dans une suite
    Par Ben_Le_Cool dans le forum Langage SQL
    Réponses: 11
    Dernier message: 28/08/2009, 18h17
  4. [griddata] Combler des trous d'interpolation
    Par raceng dans le forum MATLAB
    Réponses: 4
    Dernier message: 09/06/2009, 11h05
  5. Réponses: 2
    Dernier message: 17/01/2007, 09h01

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