How to add + x to each record returned in a SELECT [closed]

1

For example, in a select in the database, 5 records will be returned:

SELECT * FROM esc_usuarios
WHERE usu_indicador_codigo = '" . $_SESSION['codigo'] . "'
AND usu_situacao = 'ativo'"

Then I want to assign a value for each record, for example 10. Now I want to give a echo in the sum of the value of all those records, that is, 5 (select records) x 10 (value assigned for each record) , then returning the number 50. How can I do this?

Update 0: New query

SELECT usu_nome, (
          (SELECT (count(usu_codigo)*10) FROM esc_usuarios WHERE usu_indicador_codigo = a.usu_codigo AND usu_situacao = 'ativo')
          +
          (SELECT (count(usu_codigo)*5) FROM esc_usuarios WHERE usu_indicador_codigo in (
            (SELECT usu_codigo FROM esc_usuarios WHERE usu_indicador_codigo = a.usu_codigo AND usu_situacao = 'ativo')
          ))
        ) usu_porcentagem
        FROM esc_usuarios a
        WHERE usu_codigo = 1
    
asked by anonymous 18.10.2018 / 23:03

1 answer

1

Friend, if I understood your intention correctly, you would not need PHP intervention to calculate the score. You can do all the calculation on the side of the database. Here is the corresponding select and the link with the running example.

Note: I have used the id field as the primary key, switch to what you used.

This select returns all data for all users with a column with the next score. To return this column is done a sub-select to return the punctuation of the direct indication, added with the other sub-select with the indicated indication.

Select * , (
  (Select (count(id)*10) from esc_usuarios where usu_indicador_codigo = a.id)
  +
  (Select (count(id)*5) from esc_usuarios where usu_indicador_codigo in (
    (Select id from esc_usuarios where usu_indicador_codigo = a.id)
  ))
) pontos
From esc_usuarios a
Where usu_situacao = 'ativo'

If you want a specific user, enter the rule in the where as you did in your example.

AND id = '" . $_SESSION['codigo'] . "'

Follow the test link: link

    
19.10.2018 / 17:23