Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql VIEW with explicit column datatype?

Tags:

sql

mysql

How can I set the datatype of columns in a mysql view?

The following is incorrect:

CREATE VIEW person_view AS
SELECT 'Test' AS VARCHAR(20) person_firstname,
       person.age AS int(2) person_age
FROM person;

table person:

#firstname, lastname, age
john, doe, 8
jane, doe, 5

The example data will trigger the generation of VARCHAR(4) for firstname and int(1) for age. But in fact I want to support VARCHAR(20) and int(2) for those columns in the view.

like image 265
membersound Avatar asked Dec 01 '25 07:12

membersound


1 Answers

Try using CAST or CONVERT

CREATE VIEW myview AS SELECT CAST('Test' AS VARCHAR(20)) Column1 FROM mysource;
like image 91
Mark Wagoner Avatar answered Dec 02 '25 22:12

Mark Wagoner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!