Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - Select null as FIELDNAME?

I have come across some code that says

select null as UnitCost,
null as MarkUp

What exactly is this doing? Is it getting the field names unitcost and markup?

Why would you use "select null as..."?

New to this sorry.

Thanks.

like image 971
Karen Avatar asked Sep 04 '26 11:09

Karen


2 Answers

That code is aliasing the value null and calling it UnitCost/MarkUp. It is not selecting that column from any available table.

You would usually see this when a statement requires matching column sets, e.g. union all.

select id, col1, col2, null as UnitCost, null as MarkUp
from table_1
union all
select id, null as col1, null as col2, UnitCode, MarkUp
from table_2
like image 55
SqlZim Avatar answered Sep 07 '26 00:09

SqlZim


Just populate NULL to those selected columns

It is useful when you do Insert...Select, the Table you are trying to insert may have more columns than the selected table has, for convenience, you could use select null as col_name to let the numbers of columns you are trying to insert matches the columns that the target table has.

like image 40
LONG Avatar answered Sep 07 '26 01:09

LONG



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!