Jump to content

pull out first X unique fields records and rows associated with them?


yamipoli

Recommended Posts

sorry for bad phrasing but I don't know how else to explain it.

 

-----------------------------
| Fruit | Status |
-----------------------------
| Apple | Bitten |
-----------------------------
| Apple | Whole |
-----------------------------
| Orange | On Fire |
-----------------------------
| Orange | Not On Fire |
-----------------------------
| Lemon | On A Tricycle |
-----------------------------

 

 

What I want to do is find the first X fruits (assume X = 2 for this), and each row that has that key as well. So for example I want the first four rows because they are 'apple' and 'orange'. I can't specify based on Fruit name or any such easy ways out.

Link to comment
Share on other sites

i'm not sure if there is a way to do it with SQL, there probably is, but with PHP you could do it..

 

i'd do something like...

 

$last_fruit = "";
$num_fruits = 0;

while($num_fruits < 3){
  $row = mysql_fetch_assoc($result)
  if($row['fruit'] != $last_fruit){
    $last_fruit = $row['fruit'];
    $num_fruits++;
  }
  if($num_fruits < 3){
    //whatever you want to do with it
  }
}

 

of course this is after whatever sql returns the above table...

maybe not how others would do it, but it would serve your purpose

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.