Jump to content

select from two records in mysql


gerkintrigg

Recommended Posts

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.
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

No problem, do it like so:

[code]
SELECT *
FROM member AS m
JOIN pokes AS p ON p.poker = m.id OR poked = m.id
JOIN photo AS pic ON m.id = pic.id
WHERE p.poker = 123 OR p.poked = 123
GROUP 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.
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.