Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL JOIN on 2 tables with a list and column ID 1

Status
Not open for further replies.

RosieGp

Programmer
Jun 23, 2009
83
US
How can I Join on KidsData and Fruitdescription table to get Fruit Name from Fruitdescription table rather than hard coding it.
this is what I can get to this far.
Code:
 <cfquery name="kidsData">
 SELECT *
 FROM KidsData K
 LEFT JOIN Fruitdescription F ON F.ID = K.FruitData
 WHERE FruitData IS NOT NULL AND ID = #ID#
 </cfquery>

Table: KidsData
Code:
ID Name FruitData
 1 Candy 100000
 2 Andy  001000
 3 Sarah 001010
 4 Gabby NULL
 5 Jack  000110
Table: Fruitdescription
Code:
 ID FruitName 
 0 apple
 1 orange 
 2 kiwi 
 3 banana 
 4 pineapple 
 5 grape
 
Looks to me like your [tt]Fruitdescription.ID[/tt] (PK) relates to [tt]KidsData.FruitData[/tt] (FK), but the data in the tables does not look right because in one table you have values 0, 1, 2, ...5, and in another 100000, 001000, 001010, 000110
So, what is the data, what are the columns that relate one table to another?


Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Thanks Andrzejek I understand and have corrected this. I guess i just needed someone to point that out. sorry and thanks.
 
To me FruitData looks like binary data, bits set, each bit for a fruit. For that to work fruits would need ids 1,2,4,8,16... and no join type would fit that schema. To get n:m relations between kids and fruits you should design a table fruitskidslike (for example) with tuples (kidid, fruitid).

Edit: Well, normal joins would work, but you need unusual join conditions, eg KidsData.FruitData & Fruitdescription.ID = Fruitdescription.ID, besides needing a default value for ID which doubles with each new fruit would limit FruitData to 32 or 64 fruits, depending on using integer or biginteger. IDs like they are would only work as 2^ID.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top