SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast Patrick Lucas's Avatar
    Join Date
    Dec 2001
    Location
    Ohio
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    More JOIN questions

    Hello,
    I read the other active JOIN thread... and I think I have a different question.
    I have three tables (previews, reviews and news) each with mutiple columns. What I want to do is to select these columns from each table: id, title, description, date. They column name is the same in each table.
    After doing the mySQL query, the ideal result would be a table that has thoes 4 columns with all the content from the three tables, sorted by date.
    Is this possible?
    Also, this isn't as important... but when I select a row from this new 4-column table, is there a way to tell which table it came from originaly?

    Thanks for any help you can provide. Hope I explained the question clearly
    Patrick Lucas

  2. #2
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Somebody may indeed correct me here, but I'm going to give it a shot.

    Since all of the tables have the same column names, you're going to have to specify from which table you want data extracted.

    But to organize it like you want to, you're going to have to do three different queries (as far as I know); but something like should suffice:
    PHP Code:
    <?php
    $previews 
    "SELECT id, title, description, date, ".
         
    "FROM previews ORDER BY date";
    $reviews "SELECT id, title, description, date, ".
         
    "FROM reviews ORDER BY date";
    $news "SELECT id, title, description, date, ".
         
    "FROM news ORDER BY date";
    $p_query = @mysql_query($previews)
         or exit(
    "<p>An error was encountered: ".mysql_error()."</p>");
    $r_query = @mysql_query($reviews)
         or exit(
    "<p>An error was encountered: ".mysql_error()."</p>");
    $n_query = @mysql_query($news)
         or exit(
    "<p>An error was encountered: ".mysql_error()."</p>");
    echo(
    "<table>\n<tr>\n");
    while(
    $result mysql_fetch_array($p_query))
    {
         echo(
    "HTML TABLE CODING using ".$result['columnname']."HTML TABLE CODING");
    }
    while(
    $result mysql_fetch_array($r_query))
    {
         echo(
    "HTML TABLE CODING using ".$result['columnname']."HTML TABLE CODING");
    }
    while(
    $result mysql_fetch_array($n_query))
    {
         echo(
    "HTML TABLE CODING using ".$result['columnname']."HTML TABLE CODING");
    }
    echo(
    "</tr>\n</table>");
    ?>
    Joins won't work especially well for this, at least in my mind. But there's something you can do that isn't too bad on the inefficiency side.

    Although you may benefit better if you wait for someone more knowledgeable to answer. Regardless, good luck!
    Last edited by Aes; Jan 23, 2002 at 18:04.
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •