I can add a field that keeps 1 and 0 separated by semicolons

0

I have to make a sum in MySQL of a field called assistance that saves 0 and 1 depending on whether you attend or not

taller       personas                    asistencia
65           31;42;58 (idUsuario)        1;0;1;1;1

There is a way to add the attendance values to total the number of participants.

    
asked by user54766 02.08.2017 в 19:05
source

3 answers

2

Normalization

Consists of a process in which rules are applied on each of the tables to avoid redundancy of data, to reduce problems when updating data and to protect the referential integrity of data.

Problems

The structure of your database as it is now is only going to bring you problems and headaches.

  • It is impossible to ensure the referential integrity of the records, which in Spanish means that you can not know for sure if the person or workshop exists in your database.
  • Any interaction with the database is going to take many more steps than it would normally take, such as having to go through the 31;42;58 string to find a value.
  • and so I could continue ad infinitum ..

Solution

  • Create a workshop table where the id (primary key) and its name are stored.
  • Create a people table where the ID (primary key), name, etc. is stored.
  • Create an inscribed table that stores the relationship that exists between the workshops and the people, that is, that stores the two foreign keys of both the workshop and the person who attends it and a column to know whether or not he attended the workshop.

The structure would be as follows:

+-----------+    +------------+    +-----------+
+ Taller    +    + Inscripto  +    + Persona   +
+-----------+    +------------+    +-----------+
+ ID_taller +    + ID_taller  +    + DNI       +
+ Nombre    +    + DNI        +    + Nombre    +
+-----------+    + Asistio    +    +-----------+
                 +------------+

You can use the clause COUNT that returns the number of records in a query SELECT .

SELECT COUNT(*) AS cantidad
FROM inscripto
WHERE asistio = 1;

Documentation

    
answered by 02.08.2017 в 19:22
2

When asking about databases it is important to show the design to get more adequate answers. Otherwise, we will have to imagine a suitable data model.

I imagine your model more or less like this:

  • A table inscripciones that manages the people registered, in which workshop, and the attendances
  • A table personas that stores the data of people
  • A table talleres that does not appear here, but that would keep the peculiarities of each workshop

In the example I show a counter of assistants and of non assistants by workshop. I also show a list of the people who attended and those who did not, to know who are the negligent:)

You can also get the total number of registrants, saying who attended or not, etc. As you can see it is a flexible model. Imagine the same workshop that takes place in different days or sessions and you have to have attendance control per session, it would be very easy to adapt this model, either adding one or two columns to the table inscripciones ... or moving the column taller_id to the table sesiones and placing in turn a column sesion_id in the table inscripciones ... When you create a data design you must also think about the future evolution of it.

I hope it serves you.

Código: ver demo

CREATE TABLE inscripciones (
          taller_id   INT,
          persona_id  INT,
          asistio     BOOLEAN
        );

 INSERT INTO inscripciones (taller_id, persona_id, asistio) 
             VALUES 
             (65,1,TRUE),
             (65,2,TRUE),
             (65,3,FALSE),
             (65,4,TRUE),
             (65,5,FALSE),
             (65,6,TRUE),
             (65,7,TRUE)
        ;


 CREATE TABLE personas (
          persona_id  SERIAL,
          persona_nom VARCHAR(50)
        );

 INSERT INTO personas (persona_nom) 
             VALUES 
             ('Pedro'),
             ('Santiago'),
             ('Juan'),
             ('María'),
             ('Andrés'),
             ('Magdalena'),
             ('Sara')
        ;

-- Asistentes por taller

SELECT taller_id, COUNT(asistio) asistentes 
FROM inscripciones WHERE asistio=TRUE GROUP BY taller_id;

-- No Asistentes por taller


SELECT taller_id, COUNT(asistio) asistentes 
FROM inscripciones WHERE asistio=FALSE GROUP BY taller_id;

-- Asistentes al taller 65
SELECT p.persona_nom FROM personas p
INNER JOIN inscripciones i ON p.persona_id=i.persona_id
WHERE taller_id=65 AND asistio=TRUE;


-- No Asistentes al taller 65
SELECT p.persona_nom FROM personas p
INNER JOIN inscripciones i ON p.persona_id=i.persona_id
WHERE taller_id=65 AND asistio=FALSE;

Resultado:

--Total Asistentes

    taller_id   asistentes
1   65          5

--Total No Asistentes

    taller_id   no-asistentes
1   65          2



--Lista Asistentes

    persona_nom
1   Pedro
2   Santiago
3   María
4   Magdalena
5   Sara


--Lista Negligentes :)

    persona_nom
1   Juan
2   Andrés
    
answered by 02.08.2017 в 19:53
0

Maybe this instruction will help you. In this example the string "1; 0; 1; 1; 1" you have to replace for assistance.

select length (replace( replace( "1;0;1;1;1" , ";" , "") , "0","" ) ) as cantasitencia

what the instruction does is, with the replace remove the (;) and the (0) leaving only the one (1), ie "1111" to then count the length.

    
answered by 03.08.2017 в 02:54