Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql : create virtual column et set defaut value

Tags:

sql

select

mysql

I have this mysql query :

Select name from my_table

This query return me this result :

 NAME
-------
 name1
 name2
 name3

How can I create a virtual column and set a defaut value in this column ? I want this result :

 NAME   | Virtual Column
------------------------  
 name1  |    defaut_value
 name2  |    defaut_value
 name3  |    defaut_value
like image 246
prestarocket Avatar asked Jan 18 '23 21:01

prestarocket


1 Answers

Like this:

SELECT name, 'default_value' AS "A Virtual Column"
FROM my_table

This is legal this way, because in the SELECT clause you can select what so called, in the SQL standard, a value expression. Where value expression can be either of the following1:

enter image description here


1: This image from: SQL Queries for Mere Mortals(R): A Hands-On Guide to Data Manipulation in SQL

like image 134
Mahmoud Gamal Avatar answered Jan 28 '23 06:01

Mahmoud Gamal