Jump to content

SQL Select Question


warrenk

Recommended Posts

I have two tables in a one (master) to many (detail) relationship.  Is there anyway to select this as a one-to-one relationship?  I would use DISTINCT, but one of the fields I select in the detail file is not unique.  So even with the DISTINCT, it always selects multiple detail records for each master.

 

I just want one occurance of the detail record.

 

Thanks,

Warren

 

Link to comment
Share on other sites

Do you mean the field in the detail file is not identical for all rows?

 

That opens the question of which of the detail rows you want returned.  Does it matter?  If not, you can use a mysql extension:

 

SELECT mastercol, detailcol FROM master JOIN detail USING (commoncol) GROUP BY mastercol

 

This will give you some random selection from the possible values of detailcol.  Note that in standard SQL this is invalid, as detailcol is not mentioned in the group by, and is not used with an aggregate function.

 

http://dev.mysql.com/doc/refman/5.0/en/group-by-hidden-fields.html

Link to comment
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.