开发者

Join multiple tables on known ID

开发者 https://www.devze.com 2023-04-01 02:39 出处:网络
I have multiple (13 i开发者_高级运维n numbers) tables that have one thing in common: Their ID.

I have multiple (13 i开发者_高级运维n numbers) tables that have one thing in common: Their ID.

Point is that I need to query the results (can differ from none to multiple per table) for a single ID and return all results that have that ID - it's no UID.

I have one table that should/could be the "starting" point from where the other tables get connected and where the questioned ID is present in every case.

Don't want to post what I've tried so far - I want to avoid the laughes. Reading what I write in Mysql is close to see a child walking it's first steps. Cute and ... funny.

Thanks for any hint, suggestion and nice comment!


As I understand you have one parent table with ID values and many similar child tables that should be connected to parent table. Check this query. Is it what you want?

SELECT * FROM parent_table t
LEFT JOIN child_table1 t1
  ON t.id = t1.id
LEFT JOIN child_table2 t2
  ON t.id = t2.id
...
LEFT JOIN child_table<n> t<n>
  ON t.id = t<n>.id

Or this variant; in this case all child tables will be returned in rows -

SELECT * FROM parent_table t
LEFT JOIN (
  SELECT * FROM child_table1
  UNION ALL 
  SELECT * FROM child_table2
  UNION ALL 
  ...
  SELECT * FROM child_table<n>
  UNION ALL
) t1
ON t.id = t1.id
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号