How to do it in MySql?

2

You see, this time I do not have a code, nor do I want to be given a code, but simply the idea.

I have a system of posts to which I want to add a button to give points (to the selected post). Good.

But how do I make those same points that I post the post to the user who posted the post. Something like a container.

Do I have to use two sql sentences? One to insert the points in the post field in the Bd and another one to insert it in the user's accumulated?

For easy to understand:

                     ****Tabla usuario****
       ----   --------   ------     ---------
       Id     Nombre     Apellidos  Acumulado
       ----  -------    ---------   ---------
         1    Luis        Tavarez     100




                     ****Tabla posts****
       ----    ----------      --------     ---------     ---------
      Id_post   Id_usuario     Titulo       Contenido       Votos
       ----        -------    ---------     ---------      -------
        2            1         Saludos     Buenos días

As you can see, the votes field is zero, because nothing has been assigned yet. But if I assign, for example "10" points to that post, it would look like this:

                             **Tabla posts**
       ----    ----------      --------     ---------     ---------
      Id_post   Id_usuario     Titulo       Contenido       Votos
       ----        -------    ---------     ---------      -------
        2            1         Saludos     Buenos días       10

Then, the accumulated in the user table that has "100" accumulated points would summarize "10" more points and it would be like this:

                    ****Tabla usuario****
       ----   --------   ------     ---------
       Id     Nombre     Apellidos  Acumulado
       ----  -------    ---------   ---------
         1    Luis        Tavarez     110

In a few words all the points that are given to my posts are accumulated in the "Accumulated" field of the "user" table.

    
asked by luis 25.10.2016 в 00:30
source

4 answers

5
  

Use accumulators (or containers) in databases   It is never a good idea unless they can be "auditable". That is to say,   that it can be verified from some that the stored value is correct.

With this in mind, date the data structure that you pose, I would ask myself the following:

  • Can I know who voted?
  • Can I know how many points he gave him?
  • Can I prevent the same user from voting infinitely on the same post?
  • I want to be able to generate votes statistics?

Now, in response to the questions

  

Do I have to use two sql sentences? One to insert the points in the post field in the Bd and another one to insert it in the user's accumulated?

I suggest you make a detale table .

Seeing that you already have the tables users and posts , you should create a third table "posts_votes"

// TABLA posts_votos
|-------|----------|------|-----------|
|id_post|id_usuario|puntos|fecha      |
|-------|----------|------|-----------|
|1      |1         |10    |2016-10-26 |
|-------|----------|------|-----------|

This table allows:

  • Register / Save : vote of a user on a post.

    $postId = 1;
    $idUsuario = 1;
    $puntos = 1;
    $query = 'INSER INTO posts_votos (id_post, id_usuario, puntos)
        VALUES ('.$postId.', '.$idUsuario.', '.$puntos.')';
    
  • Control / Verify : if a user has already voted a post.

    $postId = 1;
    $idUsuario = 1;
    $query = 'SELECT * 
        FROM posts_votos
        WHERE id_post = '.$postId.' AND id_usuario = '.$idUsuario;
    
  • Report / Consult :

    • How many votes and points a post has.

    $postId = 1;
    $query = 'SELECT SUM(puntos) AS puntosTotal,
          COUNT(puntos) AS votosTotal
        FROM posts_votos
        WHERE id_post = '.$postId;
    
    • How many votes and points a user made.

    $idUsuario = 1;
    $query = 'SELECT SUM(puntos) AS puntosTotal,
          COUNT(puntos) AS votosTotal
        FROM posts_votos
        WHERE id_usuario = '.$idUsuario;
    
    • How many votes and points the author of the post has.

    $idAutor = 1;
    $query = 'SELECT SUM(puntos) AS puntosTotal,
          COUNT(puntos) AS votosTotal
        FROM posts_votos
        WHERE id_posts IN (
          SELECT id_post 
          FROM posts
          WHERE id_autor = '.$idAutor.'
        )';
    
    • Also, if necessary, you can know how many votes a post / author received in the last month and / or how many votes a user gave.

    $idAutor = 1;
    $query = 'SELECT SUM(puntos) AS puntosTotal,
          COUNT(puntos) AS votosTotal
        FROM posts_votos
        WHERE fecha BETWEEN "2016-10-01" AND "2016-10-31"
        AND id_posts IN (
          SELECT id_post 
          FROM posts
          WHERE id_autor = '.$idAutor.'
        )';
    
  

but if I delete that post all the points will be deleted, and I do not want any of them to be erased, but those points remain accumulated

With this table, you will never lose that info.

    
answered by 25.10.2016 в 18:03
4

I think you want to do it is the esquema of a base de datos with two tablas , something more or less like this:

// TABLA posts
|-------|----------|------|----|
|id_post|id_usuario|puntos|post|
|-------|----------|------|----|
|1      |1         |10    |POST|
|-------|----------|------|----|

// TABLA usuarios
|----------|-------|
|id_usuario|usuario|
|----------|-------|
|1         |LUIS   |
|----------|-------|

And to update the puntos in said post with respect to usuario (which is how I understand what you want) it would be with a simple query :

UPDATE posts SET puntos = 20 WHERE id_usuario = 1;
    
answered by 25.10.2016 в 01:27
3

You can make a sum in the query, you do not have to store it in the separate database, I think it would go something like that

SELECT SUM(points) as puntos FROM post WHERE user_id = 1

you could also apply an average, something like user rating, that users rate the post with stars (1 to 5 stars) and you can take an average from the stars awarded to the post among the post that has had that user

    
answered by 25.10.2016 в 17:37
1

What you ask for is done with the design you have put,

  

Do I have to use two sql statements?

You can make two queries or within the post table create a trigger that increases the Accumulated field

But as Marcos said well, in this way he is losing all kinds of traceability, from whom, when and how those points have arrived there.

Consider placing a field activo or visible in the table posts so that the posts do not erase them, you simply hide them from the user in this way:

  • traceability of who has done everything
  • if a post you really want that does not count for a vote because it does not meet certain requirements you can definitely delete it.
  • "Deleted" posts keep adding up
answered by 27.10.2016 в 08:32