简体   繁体   中英

One of SELECT statement in WHERE clause

SELECT 
    p.ID, r.ID, z.ID, 
    CONCATE_WS(' ', z.name, z.surename) AS fullname 
FROM 
    table p 
JOIN 
    table2 r 
JOIN 
    table3 z 
WHERE 
    fullname COLLAT UTF8_GENERAL_CI LIKE %John Newman% 
ORDER BY 
    p.start_date

Error:

Column 'fullname' not found in WHERE clause

you cannot use fullname alias in your where clause instead you should use

CONCATE_WS(' ', z.name, z.surename) with in your where clause.

You can only use column aliases in GROUP BY, ORDER BY, or HAVING clauses.

Standard SQL doesn't allow you to refer to a column alias in a WHERE clause. This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined.

MySQL has a convenient extension where you can use having in this context, rather than where :

SELECT p.ID, r.ID, z.ID, CONCATE_WS(' ', z.name, z.surename) AS fullname 
FROM table p JOIN
     table2 r JOIN 
      able3 z 
HAVING fullname COLLAT UTF8_GENERAL_CI LIKE %John Newman% 
ORDER BY p.start_date;

I assume that you are missing the on clauses. If you intend no on clause, then you should be explicit and use cross join stead of join .

the problem is that you are reffering to the allias in your where clause. The select statement is evaluated as last by SQL, so it doesn't know what you mean by 'fullname' and gives you an error never getting to the select part, so it never finds out what you meant by fullname.

fullname is alias while in where clause you should use column name or the same function you used to concat them CONCAT_WS(' ', z.name, z.surename)

Try this :

SELECT p.ID,r.ID,z.ID, CONCAT_WS(' ', z.name, z.surename) AS fullname 
FROM table p JOIN table2 r JOIN table3 z 
WHERE 
CONCAT_WS(' ', z.name, z.surename) COLLATE UTF8_GENERAL_CI LIKE '%John Newman%' 
ORDER BY p.start_date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM