What SQL query ?

Giganews Newsgroups
Subject: What SQL query ?
Posted by:  PL (noma…@nomail.com)
Date: Tue, 22 Aug 2006

Hi,
I have 2 tables, Companies and Contacts. Each contact belongs to a company
in that way :

Table Companies :
- id (primary key)
- other informational fields

Table Contacts :
- id (primary key)
- other informational fields
- company_id (id of the company to which the contact belongs)

Here is what I want : a list of all the companies, with only the first
contact of each company (so, only one row per company). Is it possible to
catch this with a single SQL query ?

Example:

Companies:
1 Intel
2 Microsoft

Contacts
1 John bill, company_id=1
2 tom Bom, company_id=1
3 erik Jans, company_id=2
4 jon jon, company_id=2

Magic query result :
1 Intel John Bill
2 Microsoft erik Jans.

Thanks for any help.
P.

Replies