How to select one first occurance of record accourding to filed value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KeredDrahcir
    Contributor
    • Nov 2009
    • 426

    How to select one first occurance of record accourding to filed value

    I'd like to select data from a table. One column in the table (type) can have a value of 1 to 6.
    I do different things with the data depending on the value. If the value comes up as 4, I need to look at a column (group) and then as I continue through the table, I need it to miss out any fields where the value of type is 4 and the value of group has come up before.

    Is there a way to do this in a loop.
    Code:
     $sql="SELECT * FROM table WHERE id=$id_value";
    $result=mysql_query($sql,$db) or die(mysql_error());
    while($row = mysql_fetch_array($result))
    {
      $class=new class_name($db, $table, $row['id']);
        [I]process data[/I]
    }
    Many Thanks.
  • dgreenhouse
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    I'm very unclear on what you're trying to accomplish here...

    Could you state your problem a little better?

    I understand the code you've shown, but that doesn't indicate what your issue is...

    Comment

    • KeredDrahcir
      Contributor
      • Nov 2009
      • 426

      #3
      When I runs to code it retrives the data from the table. I want to process the data differently depending upon the value of the type field.
      If the type field gives me a value of 2, I want to look at the name field and then as I continue through the table I want any occurances of data that have type of 2, and have the name used before to be dicounted, but I want all the rest of the data to be retireved as before.

      Comment

      • KeredDrahcir
        Contributor
        • Nov 2009
        • 426

        #4
        I've managed to solve the problem but if anyone else is interested in the solutions, this is the code I used:
        Code:
        $sql="SELECT [I]field1[/I], [I]field2[/I], [I]field2[/I], [I]field4[/I], MAX([I]field5[/I]) AS [I]field5[/I], [I]field6[/I], [I]field7[/I], [I]field8[/I], [I]field9[/I] FROM [I]table[/I] WHERE [I]field3[/I]=3 && [I]field9[/I]=[I]class[/i]->[I]id[/I] GROUP BY [I]field5[/I] UNION SELECT [I]field1[/I], [I]field2[/I], [I]field2[/I], [I]field4[/I], [I]field5[/I] AS [I]field5[/I], [I]field6[/I], [I]field7[/I], [I]field8[/I], [I]field9[/I] FROM [I]tabe[/I] WHERE [I]field3[/I]!=5 && [I]field3[/I]!=3 && [I]field9[/I]=[I]class[/I]->[I]id[/I] ORDER BY [I]id[/I]";

        Comment

        • KeredDrahcir
          Contributor
          • Nov 2009
          • 426

          #5
          SELECT returning wrong field

          I've found that although the previous solutions works, it doesn't return the first field on the first SELECT statement. I've got three values and it returns the second one I entered. I don't understand why.

          Code:
          SELECT field1, field2, field3, field4, MAX(field5) AS field5, field6, field7, field8, field9 FROM tables WHERE field3=3 && field9=field9=class->id GROUP BY field5 UNION SELECT field1, field2, field3, field4, field5 AS field5, field6, field7, field8, field9 FROM tables WHERE field3!=5 && field3!=3 && field9=class->id ORDER BY field1;
          The three rows that have the the 3 of three in field3 have the values of 183, 184 and 223 in field1 which were generated using an autoincrement. When I run the query row 184 comes up and I need to row with the lowest number. Can anyone suggest what to do?

          Comment

          • KeredDrahcir
            Contributor
            • Nov 2009
            • 426

            #6
            I need to join two statments together but the ORDER BY is different for each statement so I can't use UNION. Is there a way to do it. I'm trying to find something that will do the same as this:
            Code:
            SELECT * FROM table WHERE field3=3 && field9=2 ORDER BY field1 LIMIT 1 UNION SELECT * FROM table WHERE field3!=5 && field3!=3 && field9=2 ORDER BY field1;
            I know this wont work, but is there a way to get something that would generate the expected result?

            Comment

            Working...