What is the correct way to perform this query? Additionally, I’d like to trim spaces from fullname, because if middle is empty it still returns the spaces before and after it.
SELECT first,middle,last,
CONCAT(first,' ',middle,' ',last) AS fullname
FROM names a
LEFT JOIN info b ON fullname = b.name
LIMIT 1
the current error is: ERROR 1054 (42S22): Unknown column 'fullname' in 'on clause'
The problem that you are facing is that you are trying to join on a calculated column.
You need to first calculate the column in a subselect, and then join to that ‘table’
Something like
You can also have a look at the CASE Statement for the spaces in the middle.