Mysql – Is it possible to use two alieses for the same column in a query

MySQL

Context

Let's say I have a field called A of type CHAR and a field called B of type INT.

I'm migrating field A to a new type by creating a new field and copying the values around. The field is called B.

At the moment the two field coexist while I am copying the data from a column to another and the application code has a fallback so if column B contains a value then return B, otherwise return the value from A and convert the value at the application level.

The Problem

Now I would like to rename B to A but I'm finding it really difficult to find a solution that doesn't require many deploys by creating another temp column and doing a switch of columns again.

Questions

Is there anyway where I can say something like select (int(A) or B) as B from table?

Or do you have any ideas on making this a little bit smoother?

Best Answer

No you caqn't have two columns with the same name.

What you can do is

CREATE TABLE tab1(a char(2), b INT)
INSERT INTO tab1 VALUES('1',5)
ALTER TABLE tab1 CHANGE a a1 char(2)
ALTER TABLE tab1 CHANGE b a int;
ALTER TABLE tab1 CHANGE a1 b char(2)
SELECT * FROM tab1
b  |  a
:- | -:
1  |  5

db<>fiddle here