0

Running the following query

SELECT p.id as pid,p.name as pname, p.email,p.phone,p.created as pcreated,
p.updated as pupdated, GROUP_CONCAT(g.name) AS groups FROM ft_smsender_persons p 
JOIN ft_smsender_persons2groups AS p2g ON p.id = p2g.person_id JOIN ft_smsender_groups AS g ON g.id = p2g.group_id 
WHERE 1=1 AND deleted = 0 AND p.id as pid LIKE '%7%' OR pname LIKE '%7%' OR 
email LIKE '%7%' OR phone LIKE '%7%' OR pcreated LIKE '%7%' 
OR pupdated LIKE '%7%' OR groups LIKE '%7%' group by pname, email ORDER BY pid asc

gives me an error on p.id as pid LIKE '%7%'

If I use it like pid like %7%, it gives me an error saying pid is unknown column

The query is generated dynamically so I have very less scope how to fix it through my code; is there any way in mysql?

3
  • Did you try pid like '%7% ' ?? Commented Aug 12, 2015 at 18:36
  • if i run it like this SELECT p.id as pid,p.name as pname, p.email,p.phone,p.created as pcreated, p.updated as pupdated, GROUP_CONCAT(g.name) AS groups FROM ft_smsender_persons p JOIN ft_smsender_persons2groups AS p2g ON p.id = p2g.person_id JOIN ft_smsender_groups AS g ON g.id = p2g.group_id WHERE 1=1 AND deleted = 0 AND pid LIKE '%7%' OR pname LIKE '%7%' OR email LIKE '%7%' OR phone LIKE '%7%' OR pcreated LIKE '%7%' OR pupdated LIKE '%7%' OR groups LIKE '%7%' group by pname, email ORDER BY pid asc . The error it gives me is this: [Err] 1054 - Unknown column 'pid' in 'where clause' Commented Aug 12, 2015 at 18:37
  • yes, i did tried that Commented Aug 12, 2015 at 18:42

3 Answers 3

2

An alias can be used in a query select list to give a column a different name. You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column.

But,

Standard SQL disallows references to column
aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.

Details: https://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html

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

Comments

1

To clarify what Being Sunny said... You should not use an alias for a column in your where clause part of your query unless it is being referenced in an aggregate function. The query uses the where clause to determine how to narrow down the rows from the tables you have joined, using join criteria, before selecting the columns. Therefore, referring to an aliased column name in the where clause for non-aggregate function will result in an Error 1054: unknown column name.

Try removing those aliases from your query in the where clause:

SELECT p.id as pid, p.name as pname, p.email, p.phone, p.created as pcreated,
p.updated as pupdated, GROUP_CONCAT(g.`name`) AS groups FROM ft_smsender_persons p 
JOIN ft_smsender_persons2groups AS p2g ON p.id = p2g.person_id JOIN ft_smsender_groups AS g ON g.id = p2g.group_id 
WHERE 1=1 AND deleted = 0 AND p.id LIKE '%7%' OR p.`name` LIKE '%7%' OR 
p.email LIKE '%7%' OR p.phone LIKE '%7%' OR p.created LIKE '%7%' 
OR p.updated LIKE '%7%' OR `groups` LIKE '%7%' group by pname, email ORDER BY pid asc;

Comments

0

You cannot have Aliasing in WHERE clause. If this is a dynamically generated query, you can look to change the parameter that tries to give 'p.id' an alias of 'pid'. This would remove the pid aliasing in your result set, but would also remove the error you are getting.

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.