Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add extra column to sql query result with constant value

I've a simple query which returns me list of ids..

eg. select id from users;

this returns me the list of ids, but I need another x column(not for the users table or any other table in db) with some constant value which I'll give. I can achieve that with Js or other language after querying the ids. But just need to know if there's any way to do it within the query itself.

current result:

id
---
1
2
3

expected result:

id      some_new_column         
---          ---
1            abc
2            abc
3            abc

where some_new_column and abc, both should be provided in query.

Not sure if this is even possible or not. Any leads/helps appreciated.

like image 735
Mohammed Amir Ansari Avatar asked Mar 08 '26 20:03

Mohammed Amir Ansari


2 Answers

Just add the literal value to the select clause:

select id, 'abc' as some_new_column from users
like image 71
GMB Avatar answered Mar 11 '26 11:03

GMB


You just select it:

select id, 'abc' as new_column_value
from t;
like image 42
Gordon Linoff Avatar answered Mar 11 '26 09:03

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!