gerkintrigg Posted December 6, 2006 Share Posted December 6, 2006 Hi.I have a database with two tables... a member one and a vote one. I want to select the member's name from the member database where that member voted for me and I voted for the member.I can work out both halves of it, but can't work out how to put them both together.the current code i have is this:[code]$dbl_pokes_sql=mysql_query("Select member.fname, member.description, pokes.poker, pictures.url from pokes, member, pictures WHERE pictures.user_id=member.id && pokes.pop='poke' && ((member.id=pokes.poker) && (pokes.poked=".$_SESSION['my_id'].")) ORDER BY pokes.poker");[/code]Don't worry about the term "poke" it makes sense in the context of the website honest. ;) The above code only finds records from people who have "poked" me... but not that I have poked back. Is there any way to do this in the same query? or will I need to do something else? If so... umm... what?Thanks for your time. Quote Link to comment Share on other sites More sharing options...
marcus Posted December 6, 2006 Share Posted December 6, 2006 Instead of using && use ANDselect whatever from table WHERE field1=field1 AND field2=field2 Quote Link to comment Share on other sites More sharing options...
artacus Posted December 6, 2006 Share Posted December 6, 2006 [code]SELECT *FROM pokesJOIN member AS vote_by ON pokes.poker=vote_by.idJOIN member AS vote_for ON pokes.poked=vote_for.idWHERE pokes.pop='poke' AND (pokes.poker = 123 OR pokes.poked = 123)[/code] Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted December 7, 2006 Author Share Posted December 7, 2006 artacus, that seems to make sense. BUT... I now want to make sure that it's only echoing the member's picture once...Previously I used this: [code]$count_row=0;if ($dbl_pokes_sql){while($dp=mysql_fetch_array($dbl_pokes_sql)){ if ($dp['poker']!=$donotrepeat){ $count_row++; if ($count_row<5){ echo'<td valign="top" class="text" bgcolor="#FFFFFF"><div align="center">'; if ($dp['url']!=''){ echo'<a href="display.php?id='.$dp['poker'].'"> '.$dp['fname'].'<br>[/code] You get the idea...Any suggestions? Quote Link to comment Share on other sites More sharing options...
trq Posted December 7, 2006 Share Posted December 7, 2006 [quote]Instead of using && use AND[/quote]&& and AND are exactly the same in mysql. Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted December 8, 2006 Author Share Posted December 8, 2006 I know, but I didn't want to upset them. i use && because it's less to type. Quote Link to comment Share on other sites More sharing options...
artacus Posted December 9, 2006 Share Posted December 9, 2006 No problem, do it like so:[code]SELECT *FROM member AS mJOIN pokes AS p ON p.poker = m.id OR poked = m.idJOIN photo AS pic ON m.id = pic.idWHERE p.poker = 123 OR p.poked = 123GROUP BY m.id[/code]BTW. Your apps will always perform better if you let SQL do your grouping, filtering and aggregate functions. You CAN do it in PHP but thats what databases were designed to do and they do it much faster. Not to mention your code will be more maintainable. Quote Link to comment Share on other sites More sharing options...
fenway Posted December 15, 2006 Share Posted December 15, 2006 Depending on how your indexes are set up, the OR join condition and the GROUP BY may not utilize the proper indexes. Quote Link to comment 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.