Duplicate values in php

Asked

Viewed 846 times

0

I have a table in the bank called toy, inside it I have the columns name, total, quantity available and amount donated. But I want to show only the name, total and total amount donated.

When I use the code below that I know is not what I have to use, brings me everything but I have repeated toy name on this table, and I wanted it to appear only once, and the total came with the highest value and the donated amount of that toy already added up.

    if(isset($_REQUEST['rlanual']))
    {
        $ano1= $_REQUEST['rlanual'];

        $res = $mysqli->query("select * from brinquedo where ano=$ano1");
        $row1= $res->num_rows;  
         while($escrever = mysqli_fetch_array($res))
        {
            $nome1 = $escrever['nome'];
            $total = $escrever['total'];
            $qtd = $escrever['qtd_disponivel'];
            echo "$nome1";
            echo "$total";
            echo "$qtd";


        }


    }

To take the greater value of the total I have it:

$sql1=$mysqli->query('SELECT MAX(total) FROM brinquedo');
                             $result1 = $sql1;

Summing up I don’t want the same toy to appear more than once. I’ve tried even string comparison and nothing.

  • Try using GROUP BY by the name of the toy right in your query.

  • i tried using selectCount with group by but n was. always says q n can convert to string or int and i n know more q to do. so it brings me everything but as it has names repeated there not of the pq and the part of annual report.

  • I created the table directly in the database and I’m using php pages to add values.

  • but the table has the following columns: ID, NAME, TOTAL, QTD_DISPONIVEL, QTD_DOADO.

  • Total is on the same table, when you get a new toy you update the total of all? I believe that total does not need to be in the table, since a sum can be made directly by query sql...

2 answers

0


Try using the following query:

$res = $mysqli->query('SELECT * FROM brinquedo WHERE ano = ' . $ano1 . ' GROUP BY nome');

I recommend creating an index in the column name to improve the performance of the consultation.

  • has a column of id... however how is report I have to take the year I want and list everything q ta la la so q without repeating the name of the toy.

  • Leonardo, welcome! Can you explain the answer better? So it becomes more complete and useful for those who have the same problem and want to understand the answers.

  • am I with the problem. I just want the time to show the record instead of showing twice the same show only one.

0

Assuming the scenario

tabela - brinquedos

colunas
  id 
  nome
  quantidade_doada
  quantidade_disponivel

and that internal data are

id    nome        quantidade_doada    quantidade_disponível
1     bola        10                  9
2     bola        6                   6
3     carrinho    8                   8
4     blocos      80                  76
5     bola        6                   6

we can generate the response to a totalisation report as follows

SELECT nome, sum(`quantidade_disponivel`),sum(`quantidade_doada`) from brinquedos group by nome

this will generate the following result

nome      sum(`quantidade_disponivel`)  sum(`quantidade_doada`)   
blocos    76                            80
bola      21                            22
carrinho  8                             8

just use the php code you already have and print the correct variables.

  • h3nr1ke I’ll test here and then I’ll tell you if it worked. if the heat let me test today I still heheh... Thanks

  • worked out!!! Thanks if(isset($_REQUEST['rlanual'])) { $ano1= $_REQUEST['rlanual']; $res = $mysqli->query("SELECT year, name, max(total) as t1,sum(qtd_doado) as t2 from toy WHERE ano = $ano1 group by name"); $row1= $res->num_rows; while($write = mysqli_fetch_array($res)) { $total = $write['t1']; $qtd_donated = $write['t2']; echo " <tr> <td width='303' align='center'>". $write['name']." </td> <td width='303' align='center'>$total</td> <td width='304' align='center'>$qtd_donated</td> </tr> ";}}echo "</table>";

  • taking advantage I wonder if I can use Where in more than one place for example to give a select or an update where the month is null and the year is equal to the year. $result = $mysqli->query('SELECT *FROM gusj_relatorio.vestuario WHERE mes_fn is NULL, ano=$ano');

  • just include Where in select SELECT nome, sum(available amount),sum(amount donated) from brinquedos group by nome where fieldFoo = 0 AND fieldBar = 1 ae vc adjusts the values you want to test and the fields.

  • thank you so much!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.