How to set up a field using three others?

Asked

Viewed 41 times

3

I have the following fields in the database:

| id| estados | cidades | categoria |      todosjuntos      |
| 1 | mg      | sao paul| informati | mg/saopaulo/informati |

The first four are populated, but I would like to insert in the todosjuntos the same data of the 3. It is possible to do directly via SQL?

Update tabela set todosjuntos = estados / cidades / categoria Where todosjuntos=' '

Would that be the way?

1 answer

6


UPDATE
   tabela
SET
   todosjuntos = CONCAT( estados, " ", cidades, " ", categoria )
WHERE
   todosjuntos = "";

But if you’re gonna spin the first time, you don’t even need the WHERE todosjuntos = ""

Remember that you can use other characters instead of space on CONCAT.


Mysql has the CONCAT_WS also, but that allows defining the separator only once. Usually the CONCAT traditional gives more freedom to work on this issue:

todosjuntos = CONCAT_WS( "/", estados, cidades, categoria)


If you don’t use the field very often, you could simply pick up the data at select time, this way:

SELECT CONCAT( estados, " / ", cidades, " / ", categoria ) AS todos FROM tabela;
SELECT CONCAT_WS( " / ", estados, cidades, categoria ) AS todos FROM tabela;

Then you wouldn’t have to create a field just for that.

Browser other questions tagged

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