# linking column to different tables

**URL:** <https://forum.kx.com/t/linking-column-to-different-tables/7980>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [October 31, 2012, 8:40am UTC](https://forum.kx.com/t/linking-column-to-different-tables/7980 "2012-10-31T08:40:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam11](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@Sam11](https://forum.kx.com/u/Sam11)\
**Post date:** [October 31, 2012, 8:40am UTC](https://forum.kx.com/t/linking-column-to-different-tables/7980/1 "2012-10-31T08:40:00Z")

</div>

Hello,

I have a base table and multiple details table. I want to link a column of my base table to one of the details tables based on the column entry.

For example,

q)t1:( id:104 105; t:`a`b; v:4.4 5.5)

q)ta:(id:enlist 104; w:`aa)

q)tb:(id:enlist 105; w:`bb)

Now I want to create a link tlink in t1 that will link t1.id to t2.id if t=`a and t1.id to t2.id if t=`b.

This seems a bit far fetched but is this at all possible? If not, then I will create different versions of table t1 for each value of t.

---

<div class="post-metadata">

**Author:** ![Aaron\_Davies](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Aaron\_Davies](https://forum.kx.com/u/Aaron_Davies)\
**Post date:** [November 4, 2012, 11:51pm UTC](https://forum.kx.com/t/linking-column-to-different-tables/7980/2 "2012-11-04T23:51:00Z")

</div>

To: personal-kdbplus@googlegroups.comX-Mailer: Apple Mail (2.1278)\> I have a base table and multiple details table. I want to link a column of my base table to one of the details tables based on the column entry.\> \> q)t1:( id:104 105; t:`a`b; v:4.4 5.5)\> q)ta:(id:enlist 104; w:`aa)\> q)tb:([]id:enlist 105; w:`bb)\> \> Now I want to create a link tlink in t1 that will link t1.id to t2.id if t=`a and t1.id to t2.id if t=`b.i don’t think this is doable while staying in 3nflinking columns have to link to a single table to workyou could of course put the details in key-value form instead:q)td:(id:104 105;t:`a`b;w:`aa`bb)q)update tlink:`td!(`id`t#td)?([]id;t)from`t1;q)t1id t v tlink---------------104 a 4.4 0 105 b 5.5 1 q)update tlink.w from t1id t v tlink w ------------------104 a 4.4 0 aa105 b 5.5 1 bbq)but that may or may not gain you anything over just having w in t1 in the first place

---

<div class="post-metadata">

**Author:** ![raytork](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@raytork](https://forum.kx.com/u/raytork)\
**Post date:** [November 5, 2012, 2:04am UTC](https://forum.kx.com/t/linking-column-to-different-tables/7980/3 "2012-11-05T02:04:00Z")

</div>

charset=us-ascii

Message-Id: \<7CBC86A3-B898-4449-8155-38878E4905EF@gmail.com\>  
Cc: “[personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com)”   
X-Mailer: iPad Mail (10A403)  
From: Ray   
Subject: Re: [personal kdb+] linking column to different tables  
Date: Sun, 4 Nov 2012 21:04:20 -0500  
To: “[personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com)”   
  
This is a fairly common usage in SQL and could be done inside a subquery sin=  
ce the problem posed implies that the id values are unique, e.g.=20  
  
select x.id, x.text, s.text  
from tx x=20  
join=20  
(select y.id, y.text=20  
from ty y  
union  
select z.id, z.text  
from tz z  
) s  
on s.id =3D.x.id

---

<div class="post-metadata">

**Author:** ![Sam11](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@Sam11](https://forum.kx.com/u/Sam11)\
**Post date:** [November 6, 2012, 11:08pm UTC](https://forum.kx.com/t/linking-column-to-different-tables/7980/4 "2012-11-06T23:08:00Z")

</div>

Thanks for the replies. Yeah this was a ridiculous question. I ended up just creating multiple tables with common key columns that they can be joined against. Tans
