Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: MySQL question

by Juerd (Abbot)
on Mar 28, 2002 at 22:01 UTC ( #155130=note: print w/replies, xml ) Need Help??

in reply to MySQL question

Please forgive me for asking this question here, but in my estimation it seems that there are MANY monks here with extensive database experience.

Your estimation is correct. A lot of us know a big deal about cars, too. But this site isn't the appropriate place for that. Sorry, I --'ed your post.

I'd feel really bad if I only complained without trying to answer your question, so here goes:
Hmm, you didn't really state what data you would expect very clearly.

Maybe it's one of these (untested): SELECT title, doc_num, version FROM foo ORDER BY version DESC LIMIT 1 SELECT title, doc_num, MAX(version) AS maxver FROM foo GROUP BY title +ORDER BY maxver DESC


Replies are listed 'Best First'.
Re: Re: MySQL question
by Kozz (Friar) on Mar 28, 2002 at 22:21 UTC
    In the spirit of sportsman-like conduct, I ++'ed your post. Mostly the reason I venture to ask this question here is because it's a reasonable forum with intelligent persons of moderate tempers. ;) I don't get along with usenet very well, some IRC channels seem to smack of elitism, and don't know of any other online forums that would be better suited to answer my question.

    Anyhow, to answer your question, I was hoping to get one row per distinct doc_num whose version was the max() of all versions for that doc_num, and whose title corresponded to the record with the max version:

    title | doc_num | version -------+---------+-------- foo | 1234 | 2 baz | 5678 | 1
    Compare my chart above to the "original" and you may see what I mean.

    Anyhow, my apologies for tempting fate in this manner. If I've overstepped my bounds by replying to your post, I once again apologize. But if you find the breath to chastize me once more, I also welcome links to any Database-related forums. ;)

      Ahh. Then again, with a real database with subselects, that'd be:
      SELECT title, doc_num, version FROM atable WHERE (doc_num, version) IN ( SELECT doc_num, MAX(version) FROM atable GROUP BY doc_num );
      Again, this won't work in puny MySQL. PostgreSQL or better, baby!

      -- Randal L. Schwartz, Perl hacker

        If Kozz cannot upgrade to PostgreSQL for some reason couldn't Kozz ape a subselect with by creating a tempory table "ttable" and then join "atable" with that? sonething like this ...
        create table ttable as SELECT doc_num, MAX(version) FROM atable GROUP BY doc_num; SELECT title, doc_num, version FROM atable a, ttable t WHERE a.doc_num = t.doc_num and a.version = t.version;

        ... I don't know about the other monks I find SQL
        fun like its a weird puzzle.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://155130]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-05-21 05:05 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (76 votes). Check out past polls.