954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Compound MySQL Query

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?

ajbest
Light Poster
31 posts since Apr 2010
Reputation Points: 12
Solved Threads: 4
 

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
pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

i think it is:

mysql_query("SELECT a, b, (a+b) AS c FROM table HAVING c=1");
pzuurveen
Posting Whiz in Training
229 posts since Sep 2006
Reputation Points: 32
Solved Threads: 47
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: