Jump to content

using one table to subtract results from another


scarhand

Recommended Posts

ok i will try to explain this as easy as possible

 

lets say i have 2 tables

 

table 1 is called "posts" and contains these columns:

- id

- message

 

table 2 is called "opened_posts" and contains these columns:

- post_id

- reader_id

 

now lets say i want to get the results from "posts" for each row "opened_posts" does not contain the post_id.

 

i was thinking i could select all the data in "posts", create a while loop, and inside that loop select the data in "opened_posts" where the id = post_id, and if no results come up, keep looping until all rows have been gone through

 

but honestly, is there a more efficient way of doing this?

Link to comment
Share on other sites

heres what ive got so far ($myid variable has already been set):

 

<?php

$openedpostsresult = mysql_query("SELECT * FROM opened_posts WHERE reader_id = '$myid'");

while ($oprow = mysql_fetch_array($openedpostsresult))
{
  $postid = $oprow['post_id'];

  $postsresult = mysql_query("SELECT * FROM posts WHERE id != '$postid'");

  while ($prow = mysql_fetch_array($postsresult))
  {
    $prow['message'] = $message;
  
    echo $message;
  }
}

?>

 

i havent tested this out yet but it should give you an idea of what im trying to do. is there a more efficient way of coding this?

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.