I learned the quick and dirty way, so I tend to do implicit joins with some fancy (or ugly, depending on your viewpoint) where clause stuff. Here's one I made earlier with 4 tables that gets all the units in the database that have a certain $datavalue. IDs are primary keys in their own table that also exist as fields in other tables, follow the pattern of the tables and IDs for the relationship between the tables (all names munged as they wouldn't mean anything outside my project anyway)...

SELECT field1, field2, field3, field4, FROM table1, table2, table3, table4 WHERE (table1.ID_1 = table2.ID_1) AND (table2.ID_2 = table3.ID_2) AND (table3.ID_3 = table4.ID_3) AND (table3.ID_4 = $data_value);

It works for me, if the more experienced SQLMonks say its bad, don't do it.

How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist.
Robot Tourist, by Ten Benson

Edited: took out extraneous ' characters (my excuse is I pasted from Delphi, where you don't get automatic multiline strings)


In reply to Re: 2 mysql selects in one statement by robot_tourist
in thread 2 mysql selects in one statement by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.