Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a dummy column with a dummy value in SQL?

Tags:

sql

I have a table with the following

Table1 col1   col2 ------------  1      A   2      B  3      C  0      D 

Result

col1   col2  col3 ------------------ 0       D     ABC 

I am not sure how to go about writing the query , col1 and col2 can be selected by this

select col1, col2 from Table1 where col1 = 0; 

How should I go about adding a col3 with value ABC.

like image 397
kal Avatar asked Jul 07 '09 21:07

kal


People also ask

How do I select a specific row value in SQL?

To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.


2 Answers

Try this:

select col1, col2, 'ABC' as col3 from Table1 where col1 = 0; 
like image 192
Andrew Hare Avatar answered Sep 28 '22 12:09

Andrew Hare


If you meant just ABC as simple value, answer above is the one that works fine.

If you meant concatenation of values of rows that are not selected by your main query, you will need to use a subquery.

Something like this may work:

SELECT t1.col1,  t1.col2,  (SELECT GROUP_CONCAT(col2 SEPARATOR '') FROM  Table1 t2 WHERE t2.col1 != 0) as col3  FROM Table1 t1 WHERE t1.col1 = 0; 

Actual syntax maybe a bit off though

like image 44
Alex N. Avatar answered Sep 28 '22 13:09

Alex N.