Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include in SELECT a column that isn't actually in the database

Tags:

sql

select

I'm trying to execute a SELECT statement that includes a column of a static string value. I've done this in Access, but never with raw SQL. Is this possible?

Example:

 Name  | Status  ------+--------  John  | Unpaid  Terry | Unpaid  Joe   | Unpaid 

In the above example, the "Status" column doesn't exist in the database.

like image 611
Cypher Avatar asked Mar 23 '10 22:03

Cypher


People also ask

How do I select a column to exclude?

To exclude columns, you use the REPLACE() and GROUP_CONCAT() functions to generate the column names you wish to include in your SELECT statement later. You can use the result to create your SELECT statement: SELECT address,age,email,id,name,phone FROM Students; And that's how you use the information_schema.

Can we select column which is not part of group by?

The direct answer is that you can't. You must select either an aggregate or something that you are grouping by.

How do I add a static column to a select query?

Static values can be inserted into a resultset returned from a SELECT query as another column. Simply use the static value as a column to select, and the query will return a column where the name of the column is the static value, and every row in that column will return that same static value.


1 Answers

You may want to use:

SELECT Name, 'Unpaid' AS Status FROM table; 

The SELECT clause syntax, as defined in MSDN: SELECT Clause (Transact-SQL), is as follows:

SELECT [ ALL | DISTINCT ] [ TOP ( expression ) [ PERCENT ] [ WITH TIES ] ]  <select_list>  

Where the expression can be a constant, function, any combination of column names, constants, and functions connected by an operator or operators, or a subquery.

like image 180
Daniel Vassallo Avatar answered Sep 22 '22 06:09

Daniel Vassallo