[英]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:假设只有
code1
或code2
在连接中匹配,您可以尝试使用 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.