This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes simple Update fail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "simple Update fail" Watch "simple Update fail" New topic
Author

simple Update fail

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
could anyone advise why this doesn't work and what should be the correct sql?

UPDATE QA SET T_ID=48 WHERE QA_ID = (SELECT MAX(QA_ID) FROM QA WHERE Q_ID = '11' GROUP BY Q_ID)

the statments:
UPDATE QA SET T_ID=48 WHERE QA_ID = 1500
and
SELECT MAX(QA_ID) FROM QA WHERE Q_ID = '8871' GROUP BY Q_ID //returns 1500

works fine!

thanks
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

Peter,
What database are you using? Do you get an error message or does it just not update.

This may just be a typo, but the first example uses q_id = 11 and the second uses 8871.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
What's the exception/error message?

In some databases (i.e., Informix), you can't do a subselect from the same table that you are updating.
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
the err msg is:
ERROR 1064: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT

I'm using mySQL.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

Peter,
It looks like a bug in MySql.

Take a look at the comments below the manual. Search that page for "February 16 2005" and then read the next few posts.
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
mmmmm...well the problem is on a linux server having mysql 4.0.24

(it works on windows with mysql 5.0.22)

do you think upgrading to 5.0.22 on the linux machine will solve the problem?

thanks
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

I haven't encountered this problem myself. You might want to check the detailed mySql release notes to see when they fixed it.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
As a workaround you could rewrite the query as a join and see if that works.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
 
Don't get me started about those stupid light bulbs.
 
subject: simple Update fail