117

I'm trying to compile this in my mind.. i have a table with firstname and lastname fields and i have a string like "Bob Jones" or "Bob Michael Jones" and several others.

the thing is, i have for example Bob in firstname, and Michael Jones in lastname

so i'm trying to

SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast 
  FROM users 
 WHERE firstlast = "Bob Michael Jones"

but it says unknown column "firstlast".. can anyone help please ?

6 Answers 6

184

The aliases you give are for the output of the query - they are not available within the query itself.

You can either repeat the expression:

SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast 
FROM users
WHERE CONCAT(firstname, ' ', lastname) = "Bob Michael Jones"

or wrap the query

SELECT * FROM (
  SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast 
  FROM users) base 
WHERE firstLast = "Bob Michael Jones"
4
  • 6
    this was to set as answer.
    – Arun Killu
    Jan 16, 2013 at 9:35
  • after a while, i can say that i agree to use this as a better answer
    – Alex K
    May 14, 2013 at 13:01
  • @Alex you can select a different answer if you wish to do so
    – gypaetus
    Aug 8, 2013 at 19:32
  • 1
    For a bulky table with many rows, I think it would be unwise to use the "wrap the query" version. Dec 14, 2013 at 4:08
34

Try this:

SELECT * 
  FROM  (
        SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast 
        FROM users 
    ) a
WHERE firstlast = "Bob Michael Jones"
1
  • works perfect for me, thanks a lot :) and thanks for putting text in code, i forgot
    – Alex K
    Apr 20, 2011 at 18:02
10
SELECT needefield, CONCAT(firstname, ' ',lastname) as firstlast 
FROM users 
WHERE CONCAT(firstname, ' ', lastname) = "Bob Michael Jones"
8

Use CONCAT_WS().

SELECT CONCAT_WS(' ',firstname,lastname) as firstlast FROM users 
WHERE firstlast = "Bob Michael Jones";

The first argument is the separator for the rest of the arguments.

1
  • so then it should be CONCAT_WS(' ', ..
    – Alex K
    Jan 30, 2014 at 10:50
7

Try:

SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast 
  FROM users 
WHERE CONCAT(firstname, ' ', lastname) = "Bob Michael Jones"

Your alias firstlast is not available in the where clause of the query unless you do the query as a sub-select.

7

There is an alternative to repeating the CONCAT expression or using subqueries. You can make use of the HAVING clause, which recognizes column aliases.

SELECT 
  neededfield, CONCAT(firstname, ' ', lastname) AS firstlast 
FROM
  users 
HAVING firstlast = "Bob Michael Jones"

Here is a working SQL Fiddle.

2
  • Not sure why the having clause is not garnering more attention. It allows the direct use of the virtual column name. Does the having clause have more overhead?
    – Paul
    Mar 18, 2014 at 17:11
  • @Paul having clause applied in the end of executing query so we can use it to set condition on aggregate functions(like MAX()). Having clause cannot use index so it is slow. Jul 5, 2016 at 4:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.