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?

Recommended Answers

All 2 Replies

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

i think it is:

mysql_query("SELECT a, b, (a+b) AS c FROM table HAVING c=1");
commented: Correct. I keep forgetting HAVING +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.