I have 3 tables.
One is clientes
, other fotocopias
and recargas_saldo
, related by id_cliente
which is primary key in clientes
and foránea
in the others.
I need to take out the following:
1 .- Nombre, apellidos
of the table clientes
.
2.- The sum of all the copies made by the client that are in cantidad
of the table fotocopias
.
3.- The sum of all the recharges made by the client that are in cantidad_recargada
of the table recargas_saldo
.
The problem is that if I put sum
and count
I get very rare values and if I do not put it I see that I duplicate the fields ...
I've tried several sentences and nothing ...
CREATE TABLE 'clientes' (
'id_cliente' int(10) NOT NULL,
'nombre' varchar(50) COLLATE utf8_spanish_ci NOT NULL,
'apellidos' varchar(50) COLLATE utf8_spanish_ci NOT NULL,
'email' varchar(100) COLLATE utf8_spanish_ci NOT NULL,
'dni' varchar(10) COLLATE utf8_spanish_ci NOT NULL,
'telefono' varchar(15) COLLATE utf8_spanish_ci NOT NULL,
'saldo' decimal(5,2) NOT NULL,
'fecha_alta' datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
CREATE TABLE 'fotocopias' (
'id_fotocopia' int(10) NOT NULL,
'id_cliente' int(10) NOT NULL,
'tipo_fotocopia' varchar(20) COLLATE utf8_spanish_ci DEFAULT NULL,
'cantidad' int(10) DEFAULT NULL,
'precio' decimal(5,2) DEFAULT NULL,
'fecha_copias' datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
CREATE TABLE 'fotocopias_saldo' (
'id_registro' int(10) NOT NULL,
'id_cliente' int(10) NOT NULL,
'nombre_cliente' varchar(50) COLLATE utf8_spanish_ci DEFAULT NULL,
'apellido_cliente' varchar(50) COLLATE utf8_spanish_ci DEFAULT NULL,
'saldo_old' decimal(5,2) DEFAULT NULL,
'saldo_new' decimal(5,2) DEFAULT NULL,
'fecha_reg' datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
Indices de la tabla 'clientes'
--
ALTER TABLE 'clientes'
ADD PRIMARY KEY ('id_cliente');
--
-- Indices de la tabla 'fotocopias'
--
ALTER TABLE 'fotocopias'
ADD PRIMARY KEY ('id_fotocopia'),
ADD KEY 'id_cliente' ('id_cliente');
--
-- Indices de la tabla 'recarga_saldo'
--
ALTER TABLE 'recarga_saldo'
ADD PRIMARY KEY ('id_recarga'),
ADD KEY 'id_cliente' ('id_cliente');
I've tried:
select 'clientes'.'nombre' AS 'nombre','clientes'.'apellidos' AS 'apellidos',sum('fotocopias'.'cantidad') AS 'total_copias',count('recarga_saldo'.'id_cliente') AS 'recargas',sum('recarga_saldo'.'cantidad_recargada') AS 'total_recargado' from (('clientes' join 'fotocopias') join 'recarga_saldo') where (('clientes'.'id_cliente' = 'fotocopias'.'id_cliente') = 'recarga_saldo'.'id_cliente') group by 'clientes'.'id_cliente' ;
Y:
SELECT clientes.nombre, clientes.apellidos, sum(fotocopias.cantidad), count(recarga_saldo.id_cliente), sum(recarga_saldo.cantidad_recargada
FROM CLIENTES
WHERE
clientes.id_cliente = recargas_saldo.id_cliente = fotocopias.id_cliente
GROUP BY clientes.id_cliente
But it does not add well or does not take the values well, the truth is that I'm pretty rusty ...: (