scarhand Posted November 26, 2007 Share Posted November 26, 2007 looking for a select statement that can replace this: <?php $sql = mysql_query("SELECT * FROM topics"); while ($row = mysql_fetch_array($sql)) { $id = $row['id']; $readcount = mysql_num_rows(mysql_query("SELECT * FROM readtopics where topicid='$id'")); if ($readcount != 0) { echo "this topic has been read"; { } ?> any help would be greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/78998-solved-select-statement-that-does-the-same-as-this/ Share on other sites More sharing options...
teng84 Posted November 26, 2007 Share Posted November 26, 2007 SELECT count(topicid) FROM readtopics where topicid in (SELECT id FROM topics) group by topicid Quote Link to comment https://forums.phpfreaks.com/topic/78998-solved-select-statement-that-does-the-same-as-this/#findComment-399774 Share on other sites More sharing options...
scarhand Posted November 27, 2007 Author Share Posted November 27, 2007 SELECT count(topicid) FROM readtopics where topicid in (SELECT id FROM topics) group by topicid im trying to select stories that have not been read... i have a table called "stories" and a table called "stories_read" i tried this, but it does not work. i am not sure what the syntax for "NOT IN" is, i tried "!IN" but that does not work heres what ive got: the $myid variable is already declared as an integer "SELECT * FROM stories WHERE id !IN (SELECT storyid FROM stories_read) AND readerid='$myid'" might give you a better idea of what im trying to do here. "readerid" is in the stories_read table. Quote Link to comment https://forums.phpfreaks.com/topic/78998-solved-select-statement-that-does-the-same-as-this/#findComment-399906 Share on other sites More sharing options...
todding01 Posted November 27, 2007 Share Posted November 27, 2007 Maybe something like SELECT * FROM stories_read WHERE stories.id <> stories_read.id Quote Link to comment https://forums.phpfreaks.com/topic/78998-solved-select-statement-that-does-the-same-as-this/#findComment-399924 Share on other sites More sharing options...
scarhand Posted November 27, 2007 Author Share Posted November 27, 2007 i figured it out, there was an "NOT IN" syntax all along....wow...i feel dumb. here is my solution: "SELECT * FROM stories WHERE id NOT IN (SELECT storyid FROM stories_read WHERE readerid='$myid')" Quote Link to comment https://forums.phpfreaks.com/topic/78998-solved-select-statement-that-does-the-same-as-this/#findComment-399950 Share on other sites More sharing options...
fenway Posted November 27, 2007 Share Posted November 27, 2007 I think a LEFT JOIN... IS NULL would be better... Quote Link to comment https://forums.phpfreaks.com/topic/78998-solved-select-statement-that-does-the-same-as-this/#findComment-400353 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.