简体   繁体   English

根据SQL Server中的列值联接表

[英]Join table based on column value in SQL Server

Item table 项目

id  key code    description
------------------------------
1   1   misc    miscellaneous
2   1   med     medicine    

Miscellaneous table: 杂项表:

id  code    description
------------------------
1   misc1   miscellaneous
2   misc1   miscellaneous

Medicine table: 桌:

id  code        description
---------------------------
1   medicine1   medicine
2   medicine1   medicine

I have this table structure; 我有这个表结构; my main table is the Item table and I want to JOIN the main table with other table based on the column value in main table. 我的主表是Item表,我想基于主表中的列值将主表与其他表联接。 The column that determines the table to be joined is code . 确定要联接的表的列是code If code is misc join with misc table if value is med join with medicine table . 如果代码为miscmisc table连接;如果值为medmedicine table

I know the basic JOIN of table like 我知道像表的基本JOIN

SELECT * 
FROM item 
INNER JOIN miscellaneous ON item.key = miscellaneous.id

But I don't know how to join when there is a condition that will point to which table to JOIN 但是当有条件指向要JOIN表时,我不知道如何JOIN

You can use left join . 您可以使用left join Your question is vague on what should be joined with what, but it is something like this: 您的问题在将什么与什么联系在一起上含糊不清,但这是这样的:

select i.*,
       coalesce(mi.code, me.code) as code_1,
       coalesce(mi.description, me.description) as description_1
from item i left join
     miscellaneous mi
     on mi.code = i.key and i.code = 'misc' left join
     medicine me
     on me.code = i.key and i.code = 'med';

You can try a LEFT JOIN , that will be the easiest method to implement this. 您可以尝试LEFT JOIN ,这将是实现此目的的最简单方法。 But if you want both the table result to come under one column name, use UNION ALL 但是,如果希望两个表结果都在一个列名下,请使用UNION ALL

Using UNION ALL 使用UNION ALL

SELECT *
FROM item i
INNER JOIN miscellaneous m on m.code=i.code
UNION ALL
SELECT *
FROM item i
INNER JOIN medicine  me on me.code=i.code

Using LEFT JOIN 使用LEFT JOIN

SELECT *
FROM item i
LEFT JOIN miscellaneous m on m.code=i.code
LEFT JOIN medicine  me on me.code=i.code

Since Item is your 'base' / main table, you could match other tables with LEFT JOIN so that all rows from Item tables are present. 由于Item是您的“基础” /主表,因此您可以将其他表与LEFT JOIN匹配,以便出现Item表中的所有行。

SELECT * 
FROM Item AS i
LEFT JOIN Miscellaneous AS mi ON (mi.[id] = i.[key] AND i.code = 'misc')
LEFT JOIN Medicine AS me ON (me.[id] = i.[key] AND i.code = 'med');

You could also combine Miscellaneous & Medicine tables columns using ISNULL() function with some pre-condition 您还可以结合使用ISNULL()函数和其他先决条件来组合杂项和药品表列

SELECT i.*
  , ISNULL(mi.id, me.id) AS m_id
  , ISNULL(mi.code, me.code) AS m_code
  , ISNULL(mi.description, me.description) AS m_description
FROM Item AS i
LEFT JOIN Miscellaneous AS mi ON (mi.id = i.[key] AND i.code = 'misc')
LEFT JOIN Medicine AS me ON (me.id = i.[key] AND i.code = 'med');

SqlFiddle Demo SqlFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM