
Consultas con while anidadas
Publicado por Sergio (2 intervenciones) el 30/12/2014 23:06:07
Hola, tengo este código sencillo que no quiere funcionar. El primer while lista el año y según sea, suma las ventas de cada mes. Sencillo no?
El resultado es este:

que estoy haciendo mal??
Desde ya gracias por los aportes
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
<?php
require_once("../conexion/conexion.php");
?>
<html>
<table align="center" width="800px" border="1">
<tr>
<td valign="top" align="center" width="100%" colspan="13">
<b>IVA VENTAS</b>
</td>
</tr>
<tr>
<td align="center" bgcolor="red">ANO</td>
<td align="center">Ene</td>
<td align="center">Feb</td>
<td align="center">Mar</td>
<td align="center">Abr</td>
<td align="center">May</td>
<td align="center">Jun</td>
<td align="center">Jul</td>
<td align="center">Ago</td>
<td align="center">Set</td>
<td align="center">Oct</td>
<td align="center">Nov</td>
<td align="center">Dic</td>
</tr>
<?php
$sql="SELECT year(fecha)as year
from ivaventas group by year(fecha)";
$resul=mysql_query($sql,$con);
while ($regis=mysql_fetch_array($resul))
{
?>
<tr>
<?php
$sql="SELECT year(fecha)as anio,
(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='1' )as ene,
(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='2' )as feb,
(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='3' )as mar,
(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='4' )as abr,
(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='5' )as may,
(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='6' )as jun,
(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='7' )as jul,
(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='8' )as ago,
(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='9' )as sep,
(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='10' )as oct,
(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='11' )as nov,
(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='12' )as dic
from ivaventas group by year(fecha)";
$res=mysql_query($sql,$con);
while($reg=mysql_fetch_array($res))
{
?>
<td>
<?php echo $regis["year"]; ?>
</td>
<td>
<?php echo $reg["ene"]; ?>
</td>
<td>
<?php echo $reg["feb"]; ?>
</td>
<td>
<?php echo $reg["mar"]; ?>
</td>
<td>
<?php echo $reg["abr"]; ?>
</td>
<td>
<?php echo $reg["may"]; ?>
</td>
<td>
<?php echo $reg["jun"]; ?>
</td>
<td>
<?php echo $reg["jul"]; ?>
</td>
<td>
<?php echo $reg["ago"]; ?>
</td>
<td>
<?php echo $reg["sep"]; ?>
</td>
<td>
<?php echo $reg["oct"]; ?>
</td>
<td>
<?php echo $reg["nov"]; ?>
</td>
<td>
<?php echo $reg["dic"]; ?>
</td>
<?php
}
?>
</tr>
<?php
}
?>
</table>
</html>
El resultado es este:

que estoy haciendo mal??
Desde ya gracias por los aportes
Valora esta pregunta


0