mysql – How can I add data in another column with the result of another column?

Question:

I have the following scenario and I need to add the result of a Select in another column of the same table, for example, a column with the numbers and another to check how often these numbers appear:

números|frequência
 1     | 2
 2     | 1 E assim por diante. 

I managed to make a select that selects and shows the frequency, as below, but I don't know how to add the result which is the frequency in another column, because I need to use the results of these columns in the HTML .

Select to check the frequency:

select  distinct id, CONCAT(numeros, "frequencia",count(id)) as Numeros from tb_numeros group by numeros;

The result is coming out like this for now:

[{"id":1,"Numeros":"1 - frequencia - 3"},{"id":2,"Numeros":"2 - frequencia - 1"}]

Thank you very much for your attention!

Answer:

Just doing an update with a subselect in mysql would be like this

UPDATE tabela1 t1 JOIN tabela2 t2 ON t1.id = t2.id
SET t1.col1 = t2.col1, t1.col2 = t2.col2, ...

Where

   update table1 t set
column1 = (select column1 from old_table where id = t.id),
column2 = (select column2 from old_table where id = t.id);

see the way you find easier

Scroll to Top
AllEscort