Home > database >  Concat two integers into an integer in mysql
Concat two integers into an integer in mysql

Time:07-11

I have a table calle info with two columns which are ids and location, and they are both of the int type. Both of them are not primary keys. However, combinations of them is unique. Now I want to form a new column which is the concatenation of two. For instance, if my id is 12345 and my location is 12, then the resulting integer should be 1234512. Is there any way to do this in mysql?

CodePudding user response:

Here's an example of what the comments above are describing:

CREATE TABLE info (
  ids INT NOT NULL,
  location INT NOT NULL,
  -- other columns...
  UNIQUE KEY (ids, location)
);

This table rejects a row if the values in that pair of columns is already present in another row of the table. Basically, it acts as if you have a unique column containing the concatenation of the two columns.

CodePudding user response:

To answer the question as posted:

Just concatenate the fields. MySQL will cast them as appropriate.

For example:

UPDATE myTable SET newTextField = CONCAT(intField1, intField2);
  • Related