princepawn has asked for the wisdom of the Perl Monks concerning the following question:

I want a SELECT query that allows me to get the winner and loser and match_id all in single query so that I can have this data in rows. In a SQL with subqueries, this would be easy. Can you do this without temporary tables in MySQL?
select match_id,screen_name winner_name from battle,player where battl +e.winner=player.player_id; +----------+----------------------+ | match_id | winner_name | +----------+----------------------+ | 1 | Red Mustang ~PoOp~ | | 2 | Red Mustang ~PoOp~ | +----------+----------------------+
mysql> select match_id,screen_name loser_name from battle,player where + battle.loser=player.player_id; +----------+---------------------------------+ | match_id | loser_name | +----------+---------------------------------+ | 1 | Princepawn gru | | 2 | tim gru | +----------+---------------------------------+

Replies are listed 'Best First'.
Re: MySQL join of join?
by Kanji (Parson) on Sep 03, 2001 at 08:27 UTC

    Shouldn't this have been marked as off-topic?

    Anyway, in a subquery-free SQL db, you can do this on the fly by joining to the same table repeatedly as needed.

    SELECT battle.match_id , winner.screen_name AS winner_name -- <- really player.X , loser.screen_name AS loser_name -- <- really player.X FROM battle , player AS winner -- <---------------- see? same table , player AS loser -- <---------------- used twice WHERE battle.winner = winner.player_id AND battle.loser = loser.player_id;

        --k.


(Ovid) Re: MySQL join of join?
by Ovid (Cardinal) on Sep 03, 2001 at 08:36 UTC

    First, this really isn't a Perl question but I'm answering it nonetheless since SQL questions are so pertinent to what many of us do. I often ask off-topic questions, but I usually preface them with an "(OT)" so that monks who wish to avoid them can do so.

    Having said that, I'm confused as to your question. I don't know why you want a temp table. I don't use MySQL, but a quick scan of the documentation suggests that it does allow joins. Assuming that your first table is named 'winners' and the second is named 'losers', the syntax, according to the docs, would be similar to the following:

    SELECT w.match_id, w.winner_name, l.loser_name FROM winners AS w, losers AS l WHERE w.match_id = l.match_id

    I'm also a bit confused as to why winners and losers are in separate tables. Aren't both dependant on the match_id? Of course, since I don't know your needs, I could be way off base there.

    Last comment: if users of your game are completely anonymous, there's really no point in storing an arbitrary winner or loser names in the database. However, if they register, it's good to put them into a user table with a non-identifying key and have that key in your other tables. For example, if "Red Mustang" is spread out over seven or eight tables and this user decides that /s?he/ wants to be renamed "Red Lightning", you either say "no" or you search through the database for all instances of "Red Mustang", hope you don't miss any, and change them. However, if "Red Mustang" is actually user 17, you just need to change the name in the user table and all SQL automatically picks up the change. Much nicer that way.

    Again, I don't use MySQL, so this advice may be totally off base.

    Cheers,
    Ovid

    Update: Looks like Kanji has a better answer. I totally misread princepawn's post :)

    Vote for paco!

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.