MySQL - ACTUALIZAR TABLA TEMPORAL MEDIANTE INNER MYSQL

 
Vista:

ACTUALIZAR TABLA TEMPORAL MEDIANTE INNER MYSQL

Publicado por Abraham (1 intervención) el 07/04/2017 19:02:24
Hola, tengo el siguiente script

1
2
3
4
5
6
7
8
UPDATE	temp_table
	INNER JOIN temp_MAX AS TMax ON
		(	M6 = TMax.parent_id OR M5 = TMax.parent_id OR M4 = TMax.parent_id OR
			M3 = TMax.parent_id OR M2 = TMax.parent_id
		) AND (M1 < TMax.Max)
	SET	M6 = CASE WHEN M6 = TMax.parent_id THEN 0 ELSE M6 END, M5 = CASE WHEN M5 = TMax.parent_id THEN 0 ELSE M5 END,
		M4 = CASE WHEN M4 = TMax.parent_id THEN 0 ELSE M4 END, M3 = CASE WHEN M3 = TMax.parent_id THEN 0 ELSE M3 END,
		M2 = CASE WHEN M2 = TMax.parent_id THEN 0 ELSE M2 END;

Donde temp_table tiene los datos:

M1 M2 M3 M4 M5 M6
1 0 0 0 0 0
2 1 0 0 0 0
3 2 1 0 0 0
4 2 1 0 0 0
5 2 1 0 0 0
6 1 0 0 0 0
7 6 1 0 0 0
8 6 1 0 0 0
9 6 1 0 0 0
10 1 0 0 0 0
11 10 1 0 0 0
12 11 10 1 0 0
13 11 10 1 0 0
14 13 11 10 1 0

Donde temp_MAX tiene los datos:

i_row parent_id Max
1 1 14
2 2 5
3 6 9
4 10 14
5 11 14
6 13 14

El resultado que debe dar es el siguiente:

M1 M2 M3 M4 M5 M6
1 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0 0 0
4 0 0 0 0 0
5 2 0 0 0 0
6 0 0 0 0 0
7 0 0 0 0 0
8 0 0 0 0 0
9 6 0 0 0 0
10 0 0 0 0 0
11 0 0 0 0 0
12 0 0 0 0 0
13 0 0 0 0 0
14 13 11 10 1 0

El Resultado que da es:

M1 M2 M3 M4 M5 M6
1 0 0 0 0 0
2 0 0 0 0 0
3 2 0 0 0 0
4 2 0 0 0 0
5 2 0 0 0 0
6 0 0 0 0 0
7 6 0 0 0 0
8 6 0 0 0 0
9 6 0 0 0 0
10 0 0 0 0 0
11 10 0 0 0 0
12 11 10 0 0 0
13 11 10 0 0 0
14 13 11 10 1 0

Donde observamos que quita solo el valor 1 de la tabla temp_MAX y falta que quites los valores: 10,11,13

Quien me pueda ayudar como resolver esto con UPDATE SELECT, sin necesidad de CICLOS (Ya lo tengo resuelto así), se lo agradeceré.

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
/********************************************************************************************************/
SCRIPTS:
 
DROP TABLE IF EXISTS temp_MAX;
CREATE TEMPORARY TABLE temp_MAX
	(
	i_row INTEGER,
	parent_id INTEGER,
	Max INTEGER
	);
 
INSERT INTO temp_MAX (i_row, parent_id, Max)
VALUES (1, 1, 14);
 
INSERT INTO temp_MAX (i_row, parent_id, Max)
VALUES (2, 2, 5);
 
INSERT INTO temp_MAX (i_row, parent_id, Max)
VALUES (3, 6, 9);
 
INSERT INTO temp_MAX (i_row, parent_id, Max)
VALUES (4, 10, 14);
 
INSERT INTO temp_MAX (i_row, parent_id, Max)
VALUES (5, 11, 14);
 
INSERT INTO temp_MAX (i_row, parent_id, Max)
VALUES (6, 13, 14);
 
 
DROP TABLE IF EXISTS temp_table;
CREATE TEMPORARY TABLE temp_table
	(
	M1 INTEGER,
	M2 INTEGER,
	M3 INTEGER,
	M4 INTEGER,
	M5 INTEGER,
	M6 INTEGER
	);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (1, 0, 0, 0, 0, 0);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (2, 1, 0, 0, 0, 0);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (3, 2, 1, 0, 0, 0);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (4, 2, 1, 0, 0, 0);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (5, 2, 1, 0, 0, 0);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (6, 1, 0, 0, 0, 0);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (7, 6, 1, 0, 0, 0);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (8, 6, 1, 0, 0, 0);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (9, 6, 1, 0, 0, 0);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (10, 1, 0, 0, 0, 0);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (11, 10, 1, 0, 0, 0);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (12, 11, 10, 1, 0, 0);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (13, 11, 10, 1, 0, 0);
 
INSERT INTO temp_table (M1, M2, M3, M4, M5, M6)
VALUES (14, 13, 11, 10, 1, 0);
 
 
/*********************************************************************************************************/

Por favor,
Gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder