[Wolves] Happy Christmas ...and help me with this mysql query

James Turner james at turnersoft.co.uk
Thu Dec 27 17:48:22 UTC 2012

On 27/12/12 16:54, Andy Smith wrote:
> Hi Wayne,
> On Thu, Dec 27, 2012 at 04:46:53PM +0000, Wayne Morris wrote:
>> Happy Xmas ;-)
>> Now pay for it with :
> Thanks for putting the wishes first.. I've had worse:
>      http://strugglers.net/~andy/blog/2008/02/02/and-how-are-you/
> :-)
>> How do I select from
>> Item      Parts
>> Item A   Part1
>> Item A  Part2
>> --------    -------
>> ItemA   Part11
>> ItemA    Part 12
>> ItemB    Part1
>> ItemC    Part1
>> ItemD    Part 1
>> ItemD    Part 2
> I am having great difficulty understanding your table structure.

I'd imagine the above is a junction table used to resolve a many-to-many 
relationship in the schema ("each item can be composed of many parts"; 
"each part is potentially used to construct several items") and the 
"Item" and "Parts" fields are essentially foreign keys to their 
respective tables.

(see also: http://en.wikipedia.org/wiki/Associative_Entities )

>> I want to select only the first part for each item, but if I do
>> select Item where Parts like '%1%', I get Part1 and Part11 and Part
>> 12, etc

I'd guess that Andy is asking how you'd construct a query that would 
return a result set like:

{Item A,Part 1}
{Item B,Part 1}
{Item C,Part 1}
{Item D,Part 1}

...where the second field is the first-listed part identifier from the 
table (e.g. when ordered alphabetically, or however desired).



More information about the Wolves mailing list