Forum Moderators: open

Message Too Old, No Replies

Limiting MySQL Query to First Occurrence of Value

         

PaulPA

12:36 am on Jul 19, 2007 (gmt 0)

10+ Year Member



Let's say a MySQL DB has two fields (F1 and F2). The DB has many records where F2 has different values but F1 has the same value. EX:

F1 F2
1 10
1 12
1 14
2 14
2 12
2 10

In this example records 1-3 have the same F1 value but a different F2. A query contains a WHERE statement that looks for any specific instances of F2 (WHERE F2=10 OR F2=12 OR F2=14).

How can I limit a query so that once it finds the first occurrence of an F1 value (e.g., finds record #1 with F2=10) that it will then ignore that next occurences (e.g., passes over F2=12 and F2=14) and moves on to find the first occurrence of a different F1 value (finds record #4 where F1=2 and F2=14 but passes on next two records)?

phranque

5:13 am on Jul 19, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



SELECT F1, MIN(F2) FROM tablename WHERE F2=10 OR F2=12 OR F2=14 GROUP BY F1;

PaulPA

12:24 pm on Jul 19, 2007 (gmt 0)

10+ Year Member



Great! Thanks.