简体   繁体   English

表之间的条件联接?

[英]Conditional JOIN between tables?

I have two table and I should write a select query which join this two table but I do not know what is conditional join between this two tables? 我有两个表,我应该编写一个选择查询来连接这两个表,但是我不知道这两个表之间的条件连接是什么?

Can some body say what is? 有人可以说是什么吗?

TABLE ParameterRegistration
(
    RegistrationTime DATETIME,
    PatiNo VARCHAR(12),
    Source VARCHAR(64),
    Code VARCHAR(64),
    NameOfCodingSystem VARCHAR(64) NULL,
    Name VARCHAR(64),
    ValueType CHAR(2) NULL,
    NumericValue INT NULL,
    StringValue VARCHAR(64) NULL,
    TextValue TEXT NULL,
    Unit VARCHAR(64) NULL,
    UnitCode VARCHAR(64) NULL,
    UnitCodingSystem VARCHAR(64) NULL,
    Remark VARCHAR(255) NULL,
    CreateDate DATETIME,
    CreateUserId T_USER_ID
)

and

TABLE External
(
    ModDate DATETIME,
    ModUserId VARCHAR(12),
    UbMem VARCHAR(64),
    Code VARCHAR(64),
    Name VARCHAR(64),
    Service VARCHAR(64),
    NameOfCodingSystem VARCHAR(64) NULL,

)

You can only properly join those two tables if they have a relation of some kind. 如果它们具有某种关系,则只能正确地连接这两个表。

Check THIS ARTICLE for some info. 检查此文章的一些信息。


Assuming that in your case, the tables are related with the columns Code, NameOfCodingSystem and Name you can make a join like this: 假设在您的情况下,表与Code,NameOfCodingSystem和Name列相关,则可以像这样进行联接:

select p.*, e.* from ParameterRegistration p
inner join External e on p.Code = e.Code and
                         p.NameOfCodingSystem = e.NameOfCodingSystem and
                         p.Name = e.Name

When joining tables you have to join on fields that are related. 在联接表时,必须联接相关的字段。 Since you did not provide a lot of information it looks like you have 3 fields that possibly could be joined on. 由于您没有提供很多信息,因此您似乎有3个字段可以合并。

  • NameOfCodingSystem VARCHAR(64) NameOfCodingSystem VARCHAR(64)
  • Name VARCHAR(64) 名称VARCHAR(64)
  • Code VARCHAR(64) 代码VARCHAR(64)

So you could technically write your query one of these ways: 因此,您可以从技术上以下列方式之一编写查询:

SELECT *
FROM ParameterRegistration P
INNER JOIN External E -- or LEFT JOIN, etc
ON P.NameOfCodingSystem = E.NameOfCodingSystem 

OR 要么

SELECT *
FROM ParameterRegistration P
INNER JOIN External E -- or LEFT JOIN, etc
ON P.Name = E.Name 

OR 要么

SELECT *
FROM ParameterRegistration P
INNER JOIN External E -- or LEFT JOIN, etc
ON P.Code = E.Code 

OR you can join on all of the fields at the same time 或者您可以同时加入所有字段

SELECT *
FROM ParameterRegistration P
INNER JOIN External E -- or LEFT JOIN, etc
ON P.NameOfCodingSystem = E.NameOfCodingSystem  AND
P.Name = E.Name AND
P.Code = E.Code 

My suggestion would be to study up on JOINs . 我的建议是研究JOINs Here are some resources but there are plenty on the internet: 这里有一些资源,但是互联网上有很多资源:

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

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