Jump to content

Limit results in linked queries


adzie

Recommended Posts

Hello all,

 

I've inherited a rather awkward table that cannot be changed and so I need some help to limit the results.

 

Table one - member_awards

member_id

then a column for each award code

 

Table two - awards

award_id

award_desc

 


$awardCodes = mysql_query("SELECT * FROM `member_awards` WHERE `member_id` = 1");	
$awardsres = mysql_fetch_assoc($awardCodes);

$awards = mysql_query("SELECT * FROM `awards` ORDER BY 'award_id' ASC");	

while ($ac2 = mysql_fetch_assoc($awards)) {	
$ac22 = db_entry_check($awardsres[$ac2[award_id]])? 1 : 0;	
if ($ac22 != $actual && $ac22 == 1) {

// DO something

}

}


 

 

Table 1 has 20 columns, 0 if they don't have the award 1 if they do.  I need to pick out 5 where they do have the award ie the column result is 1.  This is where i'm getting stuck.  Any points would be appreciated

Link to comment
Share on other sites

You could group by and then only show the first 5 for each. This however splits your logic between the MySQL Server and PHP; not the end of the world. If you wanted all the logic in the MySQL server you've got a painful query ahead of you...

Link to comment
Share on other sites

I don't think I was very clear before hand with the table structures

 

Table one - member_awards

member_id

then a column for each award_id(in table 2) - this will be 1 if they have the award 0 if they don't

 

Table two - awards

award_id

award_desc

 

 

Link to comment
Share on other sites

so

 

member_awards =

member_id | ac1 | ac2 | ac3 | ac4 | ac5 | ac6 | ac7 | ... | ac20 |

    1    |  1  |  0  |  1  |  0  |  1  |  1  |  1  | ... |  1  |

and awards =

award_id | award_description |

  ac1  | Biggest Muppet    |

  ac2  | Worlds Best God  |

  ac3  | Zealot of the Day |

...

 

 

and you want to display 5 awards per member relating the column name from one table to the field value in another to display "Jim has recieved : award1, award20, award5, award6, award7".  is that the basics of it?

Link to comment
Share on other sites

  • 1 month later...
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.