- Home
- Categorie
- Coding e Sistemistica
- Coding
- Domanda mysql
- 
							
							
							
							
							
Domanda mysqlHo questa tabella: [TABLE="width: 500"] [TD]tipo[/TD] 
 [TD]valore[/TD]
 [TD]id[/TD]
 [/TR][TD]-1[/TD] 
 [TD][/TD]
 [TD]1[/TD]
 [/TR][TD]-2[/TD] 
 [TD][/TD]
 [TD]2[/TD]
 [/TR][TD]-2[/TD] 
 [TD][/TD]
 [TD]3[/TD]
 [/TR][TD]-1[/TD] 
 [TD][/TD]
 [TD]4[/TD]
 [/TR][TD]-2[/TD] 
 [TD][/TD]
 [TD]5[/TD]
 [/TR][TD]-1[/TD] 
 [TD][/TD]
 [TD]6[/TD]
 [/TR][TD]-2[/TD] 
 [TD][/TD]
 [TD]7[/TD]
 [/TR][TD]-2[/TD] 
 [TD][/TD]
 [TD]8[/TD]
 [/TR][TD]-2[/TD] 
 [TD][/TD]
 [TD]9[/TD]
 [/TR]
 [/TABLE]Come faccio a settare con UPDATE tutti i campi che hanno valore -2 sull'id del precedente e più vicino valore -1? Visivamente è più semplice, ecco come dovrebbe diventare: [TABLE="width: 500"] [TD]tipo[/TD] 
 [TD]valore[/TD]
 [TD]id[/TD]
 [/TR][TD]-1[/TD] 
 [TD][/TD]
 [TD]1[/TD]
 [/TR][TD]-2[/TD] 
 [TD]1[/TD]
 [TD]2[/TD]
 [/TR][TD]-2[/TD] 
 [TD]1[/TD]
 [TD]3[/TD]
 [/TR][TD]-1[/TD] 
 [TD][/TD]
 [TD]4[/TD]
 [/TR][TD]-2[/TD] 
 [TD]4[/TD]
 [TD]5[/TD]
 [/TR][TD]-1[/TD] 
 [TD][/TD]
 [TD]6[/TD]
 [/TR][TD]-2[/TD] 
 [TD]6[/TD]
 [TD]7[/TD]
 [/TR][TD]-2[/TD] 
 [TD]6[/TD]
 [TD]8[/TD]
 [/TR][TD]-2[/TD] 
 [TD]6[/TD]
 [TD]9[/TD]
 [/TR]
 [/TABLE]Grazie in anticipo! 
 
- 
							
							
							
							
							
@ruttan said: Ho questa tabella: [TABLE="width: 500"] [TD]tipo 
 [/TD]
 [TD]valore
 [/TD]
 [TD]id
 [/TD]
 [/TR][TD]-1 
 [/TD]
 [TD][/TD]
 [TD]1
 [/TD]
 [/TR][TD]-2 
 [/TD]
 [TD][/TD]
 [TD]2
 [/TD]
 [/TR][TD]-2 
 [/TD]
 [TD][/TD]
 [TD]3
 [/TD]
 [/TR][TD]-1 
 [/TD]
 [TD][/TD]
 [TD]4
 [/TD]
 [/TR][TD]-2 
 [/TD]
 [TD][/TD]
 [TD]5
 [/TD]
 [/TR][TD]-1 
 [/TD]
 [TD][/TD]
 [TD]6
 [/TD]
 [/TR][TD]-2 
 [/TD]
 [TD][/TD]
 [TD]7
 [/TD]
 [/TR][TD]-2 
 [/TD]
 [TD][/TD]
 [TD]8
 [/TD]
 [/TR][TD]-2 
 [/TD]
 [TD][/TD]
 [TD]9
 [/TD]
 [/TR]
 [/TABLE]Come faccio a settare con UPDATE tutti i campi che hanno valore -2 sull'id del precedente e più vicino valore -1? Visivamente è più semplice, ecco come dovrebbe diventare: [TABLE="width: 500"] [TD]tipo 
 [/TD]
 [TD]valore
 [/TD]
 [TD]id
 [/TD]
 [/TR][TD]-1 
 [/TD]
 [TD][/TD]
 [TD]1
 [/TD]
 [/TR][TD]-2 
 [/TD]
 [TD]1
 [/TD]
 [TD]2
 [/TD]
 [/TR][TD]-2 
 [/TD]
 [TD]1
 [/TD]
 [TD]3
 [/TD]
 [/TR][TD]-1 
 [/TD]
 [TD][/TD]
 [TD]4
 [/TD]
 [/TR][TD]-2 
 [/TD]
 [TD]4
 [/TD]
 [TD]5
 [/TD]
 [/TR][TD]-1 
 [/TD]
 [TD][/TD]
 [TD]6
 [/TD]
 [/TR][TD]-2 
 [/TD]
 [TD]6
 [/TD]
 [TD]7
 [/TD]
 [/TR][TD]-2 
 [/TD]
 [TD]6
 [/TD]
 [TD]8
 [/TD]
 [/TR][TD]-2 
 [/TD]
 [TD]6
 [/TD]
 [TD]9
 [/TD]
 [/TR]
 [/TABLE]Grazie in anticipo! Sinceramente non ho capito che vuoi fare o_o 
 Ad ogni modo è una semplice query di tipo update con clausola where
 
- 
							
							
							
							
							
Spiegarlo è più complesso che vederlo. Ecco così in grassetto magari si capisce meglio che l'id delle row con tipo uguale a -1 vieni riportato sulla colonna valore di tutte le row con tipo uguale a -2 sottostanti fino alla successiva row con tipo uguale a -1: [TABLE="width: 500"] [TD]tipo[/TD] 
 [TD]valore[/TD]
 [TD]id[/TD]
 [/TR][TD]-1[/TD] 
 [TD][/TD]
 [TD]1[/TD]
 [/TR][TD]-2[/TD] 
 [TD]1[/TD]
 [TD]2[/TD]
 [/TR][TD]-2[/TD] 
 [TD]1[/TD]
 [TD]3[/TD]
 [/TR][TD]-1[/TD] 
 [TD][/TD]
 [TD]4[/TD]
 [/TR][TD]-2[/TD] 
 [TD]4[/TD]
 [TD]5[/TD]
 [/TR][TD]-1[/TD] 
 [TD][/TD]
 [TD]6[/TD]
 [/TR][TD]-2[/TD] 
 [TD]6[/TD]
 [TD]7[/TD]
 [/TR][TD]-2[/TD] 
 [TD]6[/TD]
 [TD]8[/TD]
 [/TR][TD]-2[/TD] 
 [TD]6[/TD]
 [TD]9[/TD]
 [/TR]
 [/TABLE]
 
- 
							
							
							
							
							
Ho capito che vuoi modificare tutti i campi con tipo diverso da -1... 
 Ma che valore ci vuoi mettere? L'id dell'ultimo con -1? o_0
 
- 
							
							
							
							
							
Si come puoi vedere l'id viene riportato sulle row sottostanti fino al successivo. Purtroppo è questo che ho bisogno e questo è solo un esempio, la tabella su cui devo effettuare questa operazione è immensa, non posso farlo a mano e poi se fosse stato un semplice update l'avrei fatto da solo  
 
- 
							
							
							
							
							
Purtroppo da manuale di mysql non puoi fare una cosa del genere... 
 A prescindere che dovresti fare una subquery...
 Se la tabella fosse piccole forse potresti cercare nella sub qualcosa... il discorso però è che essendo i parametri molto variabili e senza una schema devi temo per forza passare da un linguaggio di programmazione =/