Affiliate Marketing
Forum Search

Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)  
Old 06-02-05
Dark Prince
 
Join Date: Aug 2003
Location: Moving to Windsor Woohoo!
Posts: 1,570
Thanks: 0
Thanked 9 Times in 7 Posts
lowndsy is an unknown quantity at this point
  PHP select distinct

Hi all. having a little trouble with select distinct - I need to list the cheapest unique products based on the product name - easy peasy, that's just:
"select distinct productname from wherever order by price"
but I need the contents of the rest of the row as well and you can't do that with just select distinct. Anyone know how?

Thanks in advance,
Steve
__________________
I'm on the AF Affiliate team but I'll always be a grubby affiliate at heart...

Last edited by lowndsy; 06-02-05 at 02:09 PM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-02-05
drivetowin's Avatar
Driving to win
 
Join Date: Aug 2003
Location: If I'm not at home, I'm in hospital
Posts: 7,363
Thanks: 5
Thanked 8 Times in 5 Posts
drivetowin seems to know their stuff
cant you do select distinct productname, fielda, fieldb, fieldc ..... from table order by price ?

that should still return just one row for each product
__________________
Never argue with idiots. They just drag you down to their level and then beat you with their experience.

If ignorance is bliss then some of the people I know must be orgasmic.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-02-05
Dark Prince
 
Join Date: Aug 2003
Location: Moving to Windsor Woohoo!
Posts: 1,570
Thanks: 0
Thanked 9 Times in 7 Posts
lowndsy is an unknown quantity at this point
Nope. Select distinct limits you to one field from the row. I think I either need to do a join or use MIN - I'm having another go at it now.
__________________
I'm on the AF Affiliate team but I'll always be a grubby affiliate at heart...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-02-05
999gi's Avatar
aka Antony
 
Join Date: Aug 2003
Location: Bristol
Posts: 971
Thanks: 0
Thanked 0 Times in 0 Posts
999gi is an unknown quantity at this point
Why dont you just drop the distinct and do

SELECT field1,field2,field3 FROM table WhERE condition ORDER BY price ASC LIMIT 1

That'll be the first one
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #5 (permalink)  
Old 07-02-05
Dark Prince
 
Join Date: Aug 2003
Location: Moving to Windsor Woohoo!
Posts: 1,570
Thanks: 0
Thanked 9 Times in 7 Posts
lowndsy is an unknown quantity at this point
I'll give that a go - I've got this at the moment:

SELECT gamename, price FROM game WHERE active=1 GROUP by gamename, price ORDER by price ASC

but I've seen some worrying reports on how group is just weird and doesn't always pick the same result so although it seems to be working for me I'm going to move away from it as soon as possible. I'll go check your method out now...
__________________
I'm on the AF Affiliate team but I'll always be a grubby affiliate at heart...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 07-02-05
Dark Prince
 
Join Date: Aug 2003
Location: Moving to Windsor Woohoo!
Posts: 1,570
Thanks: 0
Thanked 9 Times in 7 Posts
lowndsy is an unknown quantity at this point
Just had a closer look at that code - unless you have something clever to put where you put "condition" it'd only return one result. I need the cheapest price for every unique product in the table so I'll have to stick with the "group" method for now.
__________________
I'm on the AF Affiliate team but I'll always be a grubby affiliate at heart...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-02-05
Jack's Avatar
Registered User
 
Join Date: Oct 2003
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
Jack is an unknown quantity at this point
Think you need a subquery. Try this:

select top 100 * from products
where productname in
(select distinct productname from products)
order by price asc


- should give you the 100 cheapest unqiue products
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #8 (permalink)  
Old 08-02-05
999gi's Avatar
aka Antony
 
Join Date: Aug 2003
Location: Bristol
Posts: 971
Thanks: 0
Thanked 0 Times in 0 Posts
999gi is an unknown quantity at this point
Oh sorry - I see what you are trying to do

One way is to select them all ordered by name then price ASC

Then as you loop through them only display the first item of the same name using an if. As they are sorted by name you just need to record the last record name field and then check that the new one is different - if it is show it, if not dont
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 08-02-05
Dark Prince
 
Join Date: Aug 2003
Location: Moving to Windsor Woohoo!
Posts: 1,570
Thanks: 0
Thanked 9 Times in 7 Posts
lowndsy is an unknown quantity at this point
Yeah I thought of that but if I got a lotof traffic it would probably bring the server to its knees so now I'm thinking of doing somethig similar in the admin section and writing the results to a fresh table for users to search. Kind of defeats the object of having a feed driven site but it'd be more reliable and reduce processing load on the server too.
The group method is up and running now but it is definitely missing some results out.
__________________
I'm on the AF Affiliate team but I'll always be a grubby affiliate at heart...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Affiliate Marketing RSS Feeds - Contact Us - Affiliate Marketing - Archive - Privacy Statement - Top

Content Relevant URLs by vBSEO 3.2.0 RC7