Re: trouble dissecting many to many relationship
Posted by: Rick James
Date: March 16, 2012 09:21AM

Table 1: `Horses`, including `horse_id` to uniquely identify the horse.
Table 2: `Markings` -- some fields:
horse_id -- not unique; for connecting (JOINing) to `Horses` (many:1)
leg -- 4 values: ENUM('right front', ...) (or whatever you like)
marking -- ENUM('sock', 'coronet', 'stocking', ...)

Example of listing all horses and their markings:
SELECT  h.name, h.sire, m.leg, m.marking
    FROM  Horses h
    JOIN  Markings m ON h.horse_id = m.horse_id
    ORDER BY  h.name, m.leg;
There would be multiple lines when there are multiple markings. And (unfortumately) no lines for horses without any markings. (See LEFT JOIN as a possible way to avoid this issue.)
GROUP_CONCAT might be a way to have one line per horse and string out the markings on that line.

Options: ReplyQuote


Subject
Written By
Posted
Re: trouble dissecting many to many relationship
March 16, 2012 09:21AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.