Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding A Column that doesn't exist in a query

I want to add a column in a query that does not exist in a table and return it as a result. So lets say TABLE_TEST has column A, B and I want to return values for A, B and C. I am trying to do

SELECT A, B, C=3 FROM TABLE_TEST

or

SELECT *, C=3 FROM TABLE_TEST

Can this be done in MySQL, Postgresel or MSSQL?

like image 665
Devin Dixon Avatar asked Apr 09 '11 14:04

Devin Dixon


People also ask

How do I add a static column in SQL?

You can add static value when you use INSERT INTO SELECT MySQL query. Write the value directly in the select statement or you can add with the help of variable which initializes the value. SET @yourVariableName − = yourstaticValue; INSERT INTO yourSecondTableName(yourColumnName1,yourColumnName2,....

How do you check if a column does not exists in SQL?

Checking Existence of the Column:COL_LENGTH() function returns the defined length of a column in bytes. This function can be used with the IF ELSE condition to check if the column exists or not.

Does not exist clause in SQL?

SQL NOT EXISTS in a subqueryNOT EXISTS is used with a subquery in the WHERE clause to check if the result of the subquery returns TRUE or FALSE. The Boolean value is then used to narrow down the rows from the outer select statement.


1 Answers

Yes, sure:

select a, b, 3 as c from table_test

That's it. It works on three db engines you've mentioned.

like image 129
Pablo Santa Cruz Avatar answered Sep 24 '22 15:09

Pablo Santa Cruz