Jump to content

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

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.