0

Can anyone tell me if the following query can be done?

mysql_query("SELECT a, b, (a+b) AS c FROM table WHERE c=1");

When I try this I get an unknown column error for c. In order to get the query to work I have had to do this

mysql_query("SELECT a, b, (a+b) AS c FROM table WHERE (a+b)=1");

This is dramatically simplified from my actual query in which c is calculated using over 10 fields and multiple where clauses. In the end, in order to get this to work, my query string is very long. It seems the first bit of code should work. I shouldn't have to do the same calculation 2 times in the same query.

I have heard some discussion on the MySQLi. Would this advanced query method be able to handle requests like this?

3
Contributors
2
Replies
5
Views
5 Years
Discussion Span
Last Post by pzuurveen
0

No, changing to mysqli will not solve this, it is just not implemented in the mysql server. What you can do is this:

SELECT * FROM (
  SELECT a, b, (a+b) AS c FROM table
) AS tmp
WHERE c = 1
1

i think it is:

mysql_query("SELECT a, b, (a+b) AS c FROM table HAVING c=1");
Votes + Comments
Correct. I keep forgetting HAVING
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.