jnfields Posted December 22, 2007 Share Posted December 22, 2007 I have a table with a couple of columns. col1 col2 col3 col1 contains unique values. I want to do a query where I choose all the rows where col2="p" let's say. There are rows in the results I get where col3 is the same info. I want to narrow down the results to just rows that contain info in col3 that doesn't exist in col3 of other rows and for those rows where the col3 value exists in other rows I want just the first entry. EXAMPLE co1 col2 col3 1 p 1 2 q 0 3 p 1 4 p 3 I have been trying to write a query that will return: col1 col2 col3 1 p 1 4 p 3 Thanks for any help you can give ??? Quote Link to comment https://forums.phpfreaks.com/topic/82799-solved-tough-query/ Share on other sites More sharing options...
BenInBlack Posted December 22, 2007 Share Posted December 22, 2007 select distinct col1,col2,col3 where col3 = 'p' Quote Link to comment https://forums.phpfreaks.com/topic/82799-solved-tough-query/#findComment-421099 Share on other sites More sharing options...
jnfields Posted December 22, 2007 Author Share Posted December 22, 2007 select distinct col1,col2,col3 where col3 = 'p' No, that would return unique combinations of all the columns which in the example I gave would return all the columns. I need where col2 equals a certain value but only one of those rows returned when multiple rows have the same value in col 3. Quote Link to comment https://forums.phpfreaks.com/topic/82799-solved-tough-query/#findComment-421148 Share on other sites More sharing options...
jnfields Posted December 22, 2007 Author Share Posted December 22, 2007 select distinct col1,col2,col3 where col3 = 'p' No, that would return unique combinations of all the columns which in the example I gave would return all the columns. I need where col2 equals a certain value but only one of those rows returned when multiple rows have the same value in col 3. Ended up fixing the issue with this: SELECT col1, col2, col3 FROM table WHERE col2='p' GROUP BY col3 Worked how I wanted... Quote Link to comment https://forums.phpfreaks.com/topic/82799-solved-tough-query/#findComment-421155 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.