Jump to content

[SOLVED] Tough Query


jnfields

Recommended Posts

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 ???

Link to comment
https://forums.phpfreaks.com/topic/82799-solved-tough-query/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/82799-solved-tough-query/#findComment-421148
Share on other sites

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...

 

Link to comment
https://forums.phpfreaks.com/topic/82799-solved-tough-query/#findComment-421155
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.