MySQL - sumar cantidad en columna temporal

 
Vista:
sin imagen de perfil
Val: 26
Ha aumentado su posición en 3 puestos en MySQL (en relación al último mes)
Gráfica de MySQL

sumar cantidad en columna temporal

Publicado por Alejandro (12 intervenciones) el 29/05/2020 09:32:22
Hola buenos dias a todos,

vereis tengo una tabla MYSQL con la siguiente estructura:

1
2
3
4
5
6
7
8
9
10
11
12
CREATE TABLE `becas` (
 
`id_beca` int(10) NOT NULL AUTO_INCREMENT,
`nombre_beca` varchar (50) NOT NULL,
`cuantia` decimal(10,2) NOT NULL,
`año` year(4) NOT NULL,
`codigo_persona_beca` int(10) NOT NULL,
PRIMARY KEY (`id_beca`),
KEY `fk_personal_beca`(`codigo_persona_beca`),
FULLTEXT KEY `nombre_beca` (`nombre_beca`),
CONSTRAINT `fk_personal_beca` FOREIGN KEY (`codigo_persona_beca`) REFERENCES `datos_personales` (`id_personal`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4

Y con PHP muestro los datos en mi pagina con este Script:

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
<?php
include("conexion.php");
$id=$_POST['id'];
 
?>
 
<h3>Buscar</h3>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" autocomplete="off">
<input type="hidden" value="<?php echo $id ?>" name="id">
 
     <input type="search" name="busqueda"><input type="submit" value="Buscar">
 
</form>
<?php
 
   $busqueda = (isset($_POST['busqueda']))?$_POST['busqueda']:"";
 
if($busqueda<>''){
 
$trozos = explode("",$busqueda);
$numero = count($trozos);
 
  if($numero==1){
 
     $SQL = "SELECT nombre_beca, cuantia, año FROM tabla WHERE nombre_beca LIKE '%$busqueda%' OR año=$busqueda LIMIT 50";
     $resultSQL =  mysqli_query ($GLOBALS['conn'], $SQL);
     while($fila = mysqli_fetch_array($resultSQL)){
 
        echo "
          <table>
          <tr>
            <th>Nombre de Beca</th>
            <th>Cuantía</th>
            <th>Año</th>
          </tr>
          <tr>
            <td>".$fila['nombre_beca']."</td>
            <td>".$fila['cuantia']."</td>
            <td>".$fila['año']."</td>
          </tr>
          </table>
           ";
 
       }
 
     }else if($numero>1) {
 
       $SQL = "SELECT MATCH (nombre_beca) AGAINST ('busqueda') AS nombre_beca, cuantia, año FROM tabla MATCH (nombre_beca) AGAINST ('$busqueda') OR año=$busqueda ORDER BY nombre_beca DESC LIMIT 50";
 
       $resultSQL =  mysqli_query ($GLOBALS['conn'], $SQL);
        while($fila = mysqli_fetch_array($resultSQL)){
 
        echo "
          <table>
          <tr>
            <th>Nombre de Beca</th>
            <th>Cuantía</th>
            <th>Año</th>
          </tr>
          <tr>
            <td>".$fila['nombre_beca']."</td>
            <td>".$fila['cuantia']."</td>
            <td>".$fila['año']."</td>
          </tr>
          </table>
           ";
 
       }else{
 
 
          echo "No has introducido nada</br>";
 
 
     }
 
     ?>

Quisiera poder sumar la cantidad de la columna "cuantia" por año y por todos los años y mostrar las dos cantidades en esos SELECT como dos columnas temporales, puesto que no necesito guardarlos. Se podria???

Gracias de antemano
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