Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add column to mysql query plus give it value

Tags:

sql

mysql

I am not sure how to do this but I have a query that has unions in it. and i want to be able to diff each one by added a column. How do I add this column and give it a value. Thanks!

Select ID,Name,SpecialColumn = 'Test'
from table where ID = 'guid';
like image 338
user516883 Avatar asked Oct 25 '11 19:10

user516883


1 Answers

Use the string literal followed by a column alias 'Test' AS SpecialColumn . This will produce the same value for all rows returned, useful for differentiating between UNION components, or filling out mismatched column numbers between UNION components when necessary.

SELECT
  ID,
  Name,
 'Test' AS SpecialColumn
FROM table
WHERE ID = 'guid';

Output (ignoring your where clause) would be something like:

ID  Name     SpecialColumn
--------------------------
1   Venkman  Test
2   Egon     Test
1   Winston  Test
3   Ray      Test
like image 126
Michael Berkowski Avatar answered Sep 26 '22 00:09

Michael Berkowski