Jump to content

Help with left join


ameyemad

Recommended Posts

Hello, I was hoping someone could give me a solution to this problem. I have 2 tables for this surf script. Table1 has the sites listed, Table2 holds the surf logs. Table2 is wiped of old records every 24 hours so it doesn't get too big. This is the code I am using:

 

$newsfr_pts=@mysql_fetch_array(mysql_query("select newsfr_sites.* from newsfr_sites left join newsfr_logs on newsfr_logs.pageid=newsfr_sites.id where newsfr_sites.hits<newsfr_sites.maxhits and newsfr_sites.username!='bob' and newsfr_sites.active='y' and newsfr_sites.approved='y' and  and newsfr_logs.username='bob' and (newsfr_logs.pageid is null or newsfr_logs.nextvisit<'$time') order by newsfr_sites.lastrun limit 1"));

 

So Table1 "newsfr_sites" has:

id, url, maxhits, hits, lastviewed, owner, approved, active, lastrun

 

and Table2 "newsfr_logs" has:

username, pageid, nextvisit

 

If you need more info please ask. Thank you!

Link to comment
Share on other sites

Firstly, you shouldn't feed one function directly into another like that, it makes things harder to debug.

 

You had two and's in your query. Formatting it a little better would help with readability.

 


$sql = "
SELECT newsfr_sites.* FROM newsfr_sites
LEFT JOIN newsfr_logs ON newsfr_logs.pageid = newsfr_sites.id
WHERE newsfr_sites.hits < newsfr_sites.maxhits
    AND newsfr_sites.username!='bob'
    AND newsfr_sites.active='y'
    AND newsfr_sites.approved='y'
    AND newsfr_logs.username='bob'
    AND (newsfr_logs.pageid is null OR newsfr_logs.nextvisit < '$time')
ORDER BY newsfr_sites.lastrun limit 1;
";

if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $newsfr_pts = mysql_fetch_array($result);
  } else {
    echo "No results found";
  }
} else {
  echo "Query failed<br />$sql<br />" . mysql_error();
}

 

Does this help?

Link to comment
Share on other sites

Looks like this has not been solved at all :(

 

The code was working, and I *thought* everything was great, but that was until about an hour later. Looks as though this code will work, but it will not find a match for

pageid is null and username='bob'

. If all pages have been surfed, no matter what member surfs them, the code will not find a match.

 

So, for example (as I don't think I'm explaining myself clearly). The code that thorpe offered sort of works. But, let's say I'm a member and the only one surfing, it will let me surf every available page not a problem, but then will not allow any other member to surf as it can't find that segment of code I pasted above.

 

I hope you all understand.

Link to comment
Share on other sites

SELECT newsfr_sites.* FROM newsfr_sites
LEFT JOIN newsfr_logs ON ( newsfr_logs.pageid = newsfr_sites.id AND newsfr_logs.username='bob' AND newsfr_logs.nextvisit < '$time')
WHERE newsfr_sites.hits < newsfr_sites.maxhits
    AND newsfr_sites.username!='bob'
    AND newsfr_sites.active='y'
    AND newsfr_sites.approved='y'
    AND (1=1 OR newsfr_logs.pageid is null )
ORDER BY newsfr_sites.lastrun limit 1

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.