fabrizio
fabrizio

Reputation: 259

Use fake column on where statement mysql

I have made a query of example for select some user and I created a fake column with the value 1 for use it on the where statement but it doesn't work. The error got is

unknown column type_user

Example:

table_user

id | name | surname | type_user /* fake column */
1    Jhon   Smith        1
2    Elis   Sun          1

in select something like that:

SELECT id,name,surname, 1 as type_user 
WHERE type_user = 1

I know that with this logic is very stupid do this query but I want to understand how I can use the fake column on where.

Upvotes: 1

Views: 715

Answers (2)

Hai Handsome
Hai Handsome

Reputation: 1

You can use HAVING instead of WHERE

SELECT id,name,surname, 1 as type_user HAVING type_user = 1

Upvotes: 0

Naftali
Naftali

Reputation: 146310

You can try doing it as a subquery:

SELECT * FROM (
    SELECT id,name,surname, 1 as type_user
    FROM table_name
) as `ta`
WHERE ta.type_user = 1;

Upvotes: 6

Related Questions