Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-05-2007, 12:55 PM   PM User | #1
xiaodao
Regular Coder

 
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
xiaodao is an unknown quantity at this point
how to do this

Hi

i have a table `product` with structure like

productid productname catid
1 apple 1
2 pear 1
3 pork 2
4 beef 2
5 mutton 2
6 cake 3
7 bread 3


how can i get only one record from each catid '1' , '2' , '3' either randomly or most new one? thanks
__________________
flying dagger
xiaodao is offline   Reply With Quote
Old 12-05-2007, 12:58 PM   PM User | #2
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
Code:
SELECT DISTINCT column_name(s)
FROM table_name
__________________

nikos101 is offline   Reply With Quote
Old 12-11-2007, 01:51 PM   PM User | #3
bazz
Master Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 5,211
Thanks: 39
Thanked 201 Times in 197 Posts
bazz will become famous soon enoughbazz will become famous soon enough
if you want to find the newest one, I think you will need to have a field/column which has a time based value. Thiis could be the time it was added or some other relevant time.


Make sure you use the ISO date ofrmat and that you format the column to be a date instaed of a 'varchar' or 'int' for example. Formatting the column correctly is vital if you want to be compare, relate or add/subtract dates

bazz
bazz is offline   Reply With Quote
Old 12-11-2007, 02:47 PM   PM User | #4
aedrin
Senior Coder

 
Join Date: Jan 2007
Posts: 1,648
Thanks: 1
Thanked 58 Times in 54 Posts
aedrin will become famous soon enough
Quote:
Originally Posted by nikos101 View Post
Code:
SELECT DISTINCT column_name(s)
FROM table_name
This is not even helpful. It explains nothing.

Code:
SELECT productid, productname
FROM
    products p
    ,(SELECT DISTINCT catid FROM products) cat
WHERE
    p.catid = cat.catid
This might work. It uses a subquery to produce a list of categories, then it finds one product for each category. If it doesn't work, it should give you something to play around with.
aedrin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:05 AM.


Advertisement
Log in to turn off these ads.