Combinations

This is a discussion on "Combinations" within the Databases section. This forum, and the thread "Combinations are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > Databases

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jan 19th, 2006, 14:52
New Member
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Combinations

Would it be possible to generate such a query I demonstrate below?

Let's supposing I had this Table (below) and I wanted to pare-up all combinations and output as 1 whole Table.

A1, B1
A2, B2
A3, B3
A4, B4

So for example, I pare-up the first item A1 with B1 then the first item A1 (again) with B2 ..and so on to end up with (below).

A1, B1
A1, B2
A1, B3
A1, B4

A2, B1
A2, B2
A2, B3
A2, B4

A3, B1
A3, B2
A3, B3
A3, B4

A4, B1
A4, B2
A4, B3
A4, B4

Of course I my also want to set conditions as to how these pare-up, as in I my want to avoid certain parings i.e. not have A1 go with B3 for example.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jan 19th, 2006, 19:36
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Combinations

If you have a table called "self" and you want to join every row to every row, you would write something like

select * from self as papa join self as child

The worlds "papa" and "child" are arbitary - they're aliases which you then use to choose which ofthe combined records you need - for example:

select * from self as papa join self as child on papa.parent = child.sid
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jan 19th, 2006, 19:46
New Member
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Combinations

Okay, I think I get the idea - thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jan 20th, 2006, 13:46
New Member
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Combinations

Hello again I thought I'd solved it but haven't I'm pretty new to SQL so please be bare with me - do you have an further example I can look at? Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jan 20th, 2006, 18:40
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Combinations

There's a complete worked example here.

Basically, if you tell MySQL to join a table to itself it gets confused when you give it where or on clasues; those clauses are ambiguous. So you give each of the incoming table copies its own name - an alias - and you can then identify which colum on the left hand copy is to be linked to which column on the right hand copy by referring to those copies via its alias.

I know this sound a bit "circular" but then that's what you get if you're joining a table to itself. The first time I came across anyone doing this I thought "how daft" to myself, but then there ARE times that you want to do it - a table of people where you're holding information about who is the father of who is a good example.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jan 21st, 2006, 10:14
New Member
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Combinations

Thanks again for your time, that's certainly given me more to think about
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
combinations

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 16:00.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42