简体   繁体   English

如何从第二个连接表中获取 select 行,可以根据源列值的长度在 2 列中的任何一个上连接

[英]How to select rows from second join table which can be joined on either of 2 columns depending on length of source column value

I have a lookup table that has names and corresponding 2 different codes a 9-digit one and a 11-character alpha-numeric one.我有一个查找表,其中包含名称和对应的 2 个不同代码,一个 9 位代码和一个 11 个字符的字母数字代码。 The rows can be searched based on either of the 2 codes given depending on what the user selected while entering.根据用户在输入时选择的内容,可以根据给出的 2 个代码中的任何一个来搜索行。 So depending on whether the user has entered a 9-digit code or a 11-character alphanumeric code, I have to join the lookup table and accurately fetch the name.因此,根据用户输入的是 9 位代码还是 11 位字母数字代码,我必须加入查找表并准确获取名称。

Here's an example of the tables这是表格的示例

Data table数据表

+-----------+------------+
| code      | other cols |
+-----------+------------+
|112111213  | ...        |
|ABCD112233X| ...        |
|           |            |
+-----------+------------+

Lookup table查找表

+---------+-----------+------------+
|code1    |code2      |name        |
+---------+-----------+------------+
|112111213|XXXX111111Z|Name 1      |
|000000000|ABCD112233X|Name 2      |
|         |           |            |
+---------+-----------+------------+

How can I get the result like below我怎样才能得到如下结果

+-----------+------------+
| code      | name       |
+-----------+------------+
|112111213  | Name 1     |
|ABCD112233X| Name 2     |
|           |            |
+-----------+------------+

Assuming that only either code1 or code2 would match in the join, you may try joining with OR logic:假设只有code1code2在连接中匹配,您可以尝试使用 OR 逻辑连接:

UPDATE Data d
INNER JOIN Lookup l
    ON d.code = l.code1 OR d.code = l.code2
SET
    d.name = l.name;

Note that the presence of OR in the join condition might imply that your Lookup table is not properly normalized.请注意,连接条件中OR的存在可能意味着您的Lookup表未正确规范化。 Ideally, it might be better to maintain just a single code lookup column.理想情况下,最好只维护一个代码查找列。

Edit:编辑:

I suggest refactoring your lookup table to the following:我建议将您的查找表重构为以下内容:

+------------+-----------+
|code        |name       |
+------------+-----------+
|112111213   |Name 1     |
|XXXX111111Z |Name 1     |
|000000000   |Name 2     |
|ABCD112233X |Name 2     |
+------------+-----------+

Now you may simply perform a single join against the above code column and retrieve the name , assuming that a match be found.现在您可以简单地对上述code列执行一次连接并检索name ,假设找到匹配项。 I am assuming that multiple matches would not occur.我假设不会发生多个匹配。 If they could, then you would need to add logic to handle that.如果可以,那么您将需要添加逻辑来处理它。

It can be code like this可以是这样的代码

SELECT t1.name, t2.code1 as code FROM data as t1
INNER JOIN Lookup as t2
    ON t1.code = t2.code1 
UNION ALL
SELECT t1.name, t2.code2 as code FROM data as t1
INNER JOIN Lookup as t2
    ON t1.code = t2.code2 

You can use or based join or you can use two separate query with union all .您可以使用or based join或者您可以使用两个单独的查询与union all

Query 1: or based join查询一: or based join

SELECT d.code, l.name 
FROM data_table  d
JOIN lookup_table l ON d.code = l.code1 OR d.code = l.code2;

Query 2: union all based join查询2: union all based join

SELECT d.code, l.name 
FROM data_table  d
JOIN lookup_table l ON d.code = l.code1
UNION ALL
SELECT d.code, l.name 
FROM data_table  d
JOIN lookup_table l ON d.code = l.code2; 

Note: Better use on index use union query.注意:更好地使用索引使用union查询。

PS: Based on @Tim you need to normalize the table. PS:基于@Tim,您需要对表格进行规范化。

暂无
暂无

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

相关问题 如果连接表中存在指定值,如何使用 LEFT JOIN 选择行? - How to select rows with LEFT JOIN if specified value exists in joined table? MySQL:如何根据另一列为空,从另一列中选择具有值的列? - MySQL: How to select a column with a value from another column, depending on which columns are empty? 选择取决于第二个表中的值 - SELECT depending on value from second table [SQL]如何使用LEFT JOIN和DELETE删除联接表列上所有包含NULL值的行 - [SQL]How to delete all rows containing NULL value on the joined table column using LEFT JOIN and DELETE 如果联接表中的列的值为0或不存在,如何通过联接选择行 - How to select a row through a join if a column in the joined table has value 0 or does not exist 更好的方法来选择第一个表中的所有列,并在内连接上从第二个表中选择一列 - Better way to select all columns from first table and only one column from second table on inner join 选择JOIN行,第二个表上有2个条件 - Select rows with JOIN which have 2 conditions on second table 如何根据sql中第二列和第三列之间的最大值在哪里选择列的值? - How can I select the value of a column depending on where the maximum value between a second and third column is, in sql? SQL从联接表中选择具有最大值的行 - SQL Select rows with max value from joined table 如何从与具有谓词的第二个表中的所有行匹配的表中选择行? - How to select rows from a table that matches all the rows from a second table which has predicates?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM