This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes Problem with column alias: Unknown column 'avg_rating' in 'where clause' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem with column alias: Unknown column Watch "Problem with column alias: Unknown column New topic
Author

Problem with column alias: Unknown column 'avg_rating' in 'where clause'

Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Hello,
I have a basic sql statement as follows:



I have used a column alias for "avg(r.rating)" and named it "avg_rating".

It works in my Mysql Query browser without problem but when I run it from Java I get this:



Can anyone help please?

Thanks in advance,

Julien Martin.
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
use 'as' between avg(r.rating) avg.rating


The difference between <b>failure</b> and <b>success</b> is often being <b>right</b> and being <b>exactly right</b>.
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Hello Muhammad,
Thanks for the reply. However I still get the same exception after introducing the change: avg(r.rating) as avg_rating


any other idea?

Julien
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
well, looking at the errors, the where clause is unable to recognize the column.

replace the avg_rating >= 1 to avg(rt.rating).

You can't use the alias in "where" clause, the where clause must be provided with the column names already defined in database.

I hope this will work.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with column alias: Unknown column 'avg_rating' in 'where clause'
 
Similar Threads
Exception propogation in Jboss EJB.
EJB3.0 giving java.lang.ClassCastException: $Proxy81
JBilling trowing exception with JBoss3.x
hibernate many to one question