MySQL SELECT To Add A New Column To A Query And Give It A Value?

  • Home
  • Whiteboard
  • Online Compilers
  • Practice
  • Articles
  • AI Assistant
  • Jobs
  • Tools
  • Corporate Training
  • Courses
  • Certifications
Menu Categories Login
  • Switch theme
  • SQL
  • HTML
  • CSS
  • Javascript
  • Python
  • Java
  • C
  • C++
  • PHP
  • Scala
  • C#
  • Tailwind CSS
  • Node.js
  • MySQL
  • MongoDB
  • PL/SQL
  • Swift
  • Bootstrap
  • R
  • Machine Learning
  • Blockchain
  • Angular
  • React Native
  • Computer Fundamentals
  • Compiler Design
  • Operating System
  • Data Structure and Algorithms
  • Computer Network
  • DBMS
  • Excel
Technical Questions and Answers
  • Data Structure Data Structure
  • Networking Networking
  • RDBMS RDBMS
  • Operating System Operating System
  • Java Java
  • MS Excel MS Excel
  • iOS iOS
  • HTML HTML
  • CSS CSS
  • Android Android
  • Python Python
  • C Programming C Programming
  • C++ C++
  • C# C#
  • MongoDB MongoDB
  • MySQL MySQL
  • Javascript Javascript
  • PHP PHP
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who
MySQL SELECT to add a new column to a query and give it a value? MySQLMySQLi Database

To add column to MySQL query and give it a value, use the below syntax −

select yourColumnName1,yourColumnName2,.....N ,yourValue AS anyAliasName from yourTableName;

Let us first create a table −

mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(20) ); Query OK, 0 rows affected (0.84 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(FirstName) values('Larry'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.15 sec)

Following is the query to display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 2 | Larry | | 3 | Chris | | 4 | Robert | +----+-----------+ 4 rows in set (0.00 sec)

Following is the query to add column to MySQL and give it a value. Here we have set the value 23 after creating a new column AGE −

mysql> select Id,FirstName,23 AS AGE from DemoTable;

This will produce the following output −

+----+-----------+-----+ | Id | FirstName | AGE | +----+-----------+-----+ | 1 | John | 23 | | 2 | Larry | 23 | | 3 | Chris | 23 | | 4 | Robert | 23 | +----+-----------+-----+ 4 rows in set (0.00 sec) Ankith Reddy Ankith Reddy Updated on: 2019-07-30T22:30:25+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started Print Page Previous Next Advertisements

Tag » Add Column Mysql Query