How do I make mysql_num_rows of various results

Asked

Viewed 263 times

6

Well, what I want is this:

I have 1 table designated by items, in this table I have 4 columns:

  • nomeitem
  • iditem (Single id)
  • numerobot

What I intend to do is: I want to do a mysql_num_rows of all results, but for each different item name.

That is to say I have 10 results with the name "Shadow Case", 15 results with the name "Gamma Case".

What I intend to do is to do this, so that you know the amount of each item I have.

To finish I intend to take these values, and update another table that is called infoitems. This table only has 2 columns that are item and quantity, where in the quantity I intend to put the respective previous result, for each item.

I hope you understand me.

  • I just don’t understand why you feed another table and you can run a script to get this data at any time. These data will not be consolidated in the table?

2 answers

4


Can be done directly in a query in Mysql:

SELECT nomeitem, count(nomeitem) as quantidade FROM tabela GROUP BY nomeitem;

With this you have the amount of each item.

You can take the result in a Mysqli_result, or inside you can update directly to another table:

INSERT INTO outra_tabela (nomeitem, quantidade) 
SELECT nomeitem, count(nomeitem) as quantidade FROM tabela GROUP BY nomeitem;
  • Because that "amount" is in Lectlect?

  • 2

    on "Count(item name) quantity", see here: https://www.w3schools.com/sql/sql_alias.asp

3

Browser other questions tagged

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