If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Using alias in WHERE

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-09-10, 11:21
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Using alias in WHERE

Hi everyone,

I've an SQL as follows:

Quote:
SELECT ROUND((correct/completed) * 100, 2) AS percent_score FROM records WHERE member_id=10 AND percent_score > 50.00;

I get the error ERROR 1054 (42S22): Unknown column 'percent_score' in 'where clause'

How do I make the second condition "percent_score > 50.00" work?

Looking forward to your replies
Reply With Quote
  #2 (permalink)  
Old 07-09-10, 11:29
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Since the column is calculated in the select, there is no way to have it included in your where clause. What you can do is materialize that resultset, then you can query it. Something like:

Code:
select percent_score
   from (SELECT ROUND((correct/completed) * 100, 2) AS percent_score
               FROM records WHERE member_id=10) as a
where percent_score > 50.00;
Dave
Reply With Quote
  #3 (permalink)  
Old 07-09-10, 11:44
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Thank you so much, Dave

It works now
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On