php - mysql how to show some rows of table depending on one field and depending on one field in other table? -
i have script members login , read posts catagory have in table called posts, click button , entry inserted table called postsread collecting postname, memberid, , date showing had been read them. looking query display them posts have not read.
**tables** **fields** posts id, name, date, from, topic, info, cat postsread id, postname, memberid, date users id, memberid, pass, fname, lname, email
sessions holding $_session['memberid'], unsure of how query between 2 tables i'm looking for. like: show posts in posts except in postsread members memberid next corresponding postname. using php version 5.3, , mysql 5.0.96.
i using following display posts database:
$sql=mysql_query("select * posts cat='1' order date desc");
but not differentiate between if member has clicked stating have seen them yet or not.
i have looked many places , see examples close cant fit needing. don't understand how write this. have tried many no success. if need description please ask. thank time.
you need construct join
condition against posts_read
table, or use in
clause exclude them. should careful indexes these sorts of queries can extremely slow , database intensive non-trivial amounts of data.
select * posts cat=:cat , name not in (select postname postsread memberid=:memberid)
here :cat
, :memberid
placeholders appropriate values. using pdo can bind directly using execute
function.
as note, joining on strings lot slower joining on id
type values. might want make postsread
table reference id
posts
instead.
Comments
Post a Comment