Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MySQL SELECT statement, how can a derived field utilise the value of another field in the SELECT list?

Tags:

sql

mysql

In a MySQL SELECT statement, how can a derived field utilise the value of another field in the SELECT list?

For example, when running the following query:

SELECT
 'tim' AS first_name
,first_name || ' example' AS full_name;

I would expect the result to be:

first_name, full_name
tim       , tim example

Instead, I get the following error:

Unknown column 'first_name' in 'field list'.

Is there a way I can reference another column?

Thanks
Turgs

like image 748
Turgs Avatar asked Dec 02 '22 00:12

Turgs


1 Answers

No, you have to repeat it or use a derived table.

select *, concat(first_name,  ' example') as full_name
 from (
select
 'tim' as first_name ) as t
like image 157
Nicola Cossu Avatar answered Feb 15 '23 22:02

Nicola Cossu