MySQL SELECT To Add A New Column To A Query And Give It A Value?
Maybe your like
- Home
- Whiteboard
- Online Compilers
- Practice
- Articles
- AI Assistant
- Jobs
- Tools
- Corporate Training
- Courses
- Certifications
- Switch theme
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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
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 Updated on: 2019-07-30T22:30:25+05:30 13K+ Views
Kickstart Your Career
Get certified by completing the course
Get StartedTag » Add Column Mysql Query
-
How To Add Columns To A Table Using MySQL ADD COLUMN
-
SQL ALTER TABLE Statement - W3Schools
-
MySQL: ALTER TABLE Statement - TechOnTheNet
-
MySQL 8.0 Reference Manual :: 13.1.9 ALTER TABLE Statement
-
MySQL Add A Column To Existing Table - Linux Hint
-
How To Add A Column In A Table In MySQL? - Tutorialspoint
-
MySQL ALTER TABLE Add Column - EduCBA
-
How To Add A Column In MySQL - PopSQL
-
MySQL Add/Delete Column - Javatpoint
-
Adding Multiple Columns AFTER A Specific Column In MySQL
-
How To Add A Column To A MySQL Table - ThoughtCo
-
Adding A Column After Another Within SQL (MySQL) - Skyvia
-
SQL Server ALTER TABLE ADD Column
-
Add A Column To Index In MySQL Table - Tutorial Kart