0

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'

1
  • 1
    Apart from the answers provided regarding the derived column join, you can use CONCAT(first, COALESCE(CONCAT(' ', middle, ' '), ' '), last) AS fullname for the fullname Commented Jul 13, 2012 at 4:31

4 Answers 4

3

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

SELECT  *
FROM    (
            SELECT  first,
                    middle,
                    last,  
                    CONCAT(first,' ',middle,' ',last) AS fullname 
            FROM    names 
        ) a  LEFT JOIN 
        info b  ON  fullname = b.name 
LIMIT 1 

You can also have a look at the CASE Statement for the spaces in the middle.

Sign up to request clarification or add additional context in comments.

3 Comments

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(first,' ',middle,' ',last) AS fullname FROM names' at line 6
Thanks, that looks great and it worked. I modified the CONCAT() line using the IF() provided by sel below, to fix my 2nd issue.
You can also do: ON CONCAT(n.first,' ',n.middle,' ',n.last) = b.name
1

You can't use an alias in the ON clause. Try doing

SELECT first,middle,last,
CONCAT(first,' ',middle,' ',last) AS fullname
FROM names a 
LEFT JOIN info b ON b.name=CONCAT(first,' ',middle,' ',last) 

"The conditional_expr used with ON is any conditional expression of the form that can be used in a WHERE clause" (from dev.mysql.com/doc/refman/5.1/en/join.html).

So as a logical inference you're not allowed to use aliases in ON clauses.

2 Comments

I was afraid I'd have to do something like that, but hoped for a more elegant answer. Any chance of tackling the consecutive spaces between first and last when a middle is empty?
SELECT IF(middle='', CONCAT(first,' ',last), CONCAT(first,' ',middle,' ',last) ) as fullname
0

Try this:

select first, last, CONCAT(first,' ',middle,' ',last) as fullNAME from names a left join info b on (CONCAT(first,' ',middle,' ',last) =b.name)

one more thing, u have skipped a comma after last in ur select query

Comments

0

try this ,

SELECT CONCAT(first,' ',middle,' ',last) AS fullname FROM names a

LEFT JOIN info b

ON fullname = b.name

LIMIT 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.