N
N
Nick Toot2016-05-14 14:24:57
MySQL
Nick Toot, 2016-05-14 14:24:57

How to set a condition on a renamed column in SQL?

Good day to all! :)
Can you please tell me how to set a condition for a renamed column in SQL?
Here's an example of how I'm trying to do it:

SELECT user_id, concat(user_name, " ", user_surname, " ", user_patronim) AS fio
FROM users
WHERE fio like 'Абв%'

I get an error: I
#1054 - Unknown column 'fio' in 'where clause'
did not find solutions on the Internet.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2016-05-14
@NickToot

in this case, fio is an alias, MySQL cannot do WHERE by aliases, so fam needs to
either rewrite the WHERE construct to
WHERE concat(user_name, " ", user_surname, " ", user_patronim) like 'Abc%'
or use the HAVING construct instead of WHERE, ex:
HAVING fio like 'Abc%'

C
cheshirerabbit, 2016-05-17
@cheshirerabbit

Let him use WHERE concat(user_name, " ", user_surname, " ", user_patronim) like 'Abc%'
Then there will be no problems with aggregation of extra data when he decides to add GROUP BY

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question