Jump to content

Help with table joins


theone

Recommended Posts

[code]
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$sql = "SELECT * FROM news_site LEFT JOIN staff ON news_site.poster = staff.id WHERE news_site.show = '1' ORDER BY news_site.posted desc LIMIT 10";
$results = mysql_query($sql);
$rowcount = mysql_num_rows($results);
$i = "0";
while ($data = mysql_fetch_array($results)) {
        $i++;
    print "<u>$data[title]</u><br>";
    print "$data[post]<br><br>";
    print DateConvert($data['posted'], "l, jS F Y @ H:i:s");
    if ($i != $rowcount) {
        print "<hr noshade size=1 color=#3366cc>";
    } else {
        unset($i);
    }
}
?>
[/code]

So thats all good, but what i also want is to be able to fetch the staff.displayname for each news_site.poster, if that makes sense.

Thanks in advance,
Dave
Link to comment
Share on other sites

Well, you're asking for "*" -- all the fields in both tables. This may be troublesome if there are column name collisions. You should explicity refer to the columns you want, and alias them as necessary. In your case, it seems like you be doing:

[code]SELECT news_site.title, news_site.post, news_site.data, staff.displayname.....[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.