在 Table1 中查找与 Table2 中的条件行匹配的行
find rows in Table1 that match criteria rows in Table2
我有两个 table:
T1
是一个table的数据
- 列
one
不能有 null
- 列
two
和 three
可以有 null
s
T2
是一个table的分类规则
- 它具有与
T1
相同的列以及 cat
列来表示类别
- 想法是前三列具有用于确定
T1
中的行应该如何分类以及是否应该分类的标准
T2
中的一行可能有 2+ 列的值,这意味着 T1
中有多个条件需要匹配(例如 T1.two like "2*" and T1.three like "hi"
)
我想要一个查询来查找 T1
中根据 T2
中的条件匹配的行。这是一个例子:
+------+------+-------+
| T1 |
+------+------+-------+
| one | two | three |
+------+------+-------+
| aaaa | 1111 | |
| bbbb | 2222 | |
| cccc | | test |
| dddd | | |
+------+------+-------+
+------+-----+-------+------+
| T2 |
+------+-----+-------+------+
| one | two | three | cat |
+------+-----+-------+------+
| aaaa | * | * | 1 | -> all rows in T1 where column one equals aaaa
| * | 2* | * | 2 | -> all rows in T1 where column two starts with 2
| * | * | test | 3 | -> all rows in T1 where column three equals test
| * | 3* | hi | 3 | -> all rows in T1 where column two starts with 3 AND column 3 equals hi
+------+-----+-------+------+
我在 T2
中得到了 *
,因为我想说这些列中的值应该无关紧要。因此,以第二行为例,我说的是匹配 T1 中的所有行,其中:
one
是什么
two
以 2 开头
three
随便什么
我的想法是进行不明确的连接,并在匹配的行上进行过滤:
SELECT T1.one, T2.one, T1.two, T2.two, T1.three, T2.three, T2.id
FROM T1, T2
WHERE
(T1.one Like [T2].[one]) ' match column one
AND (T1.two Is Null Or T1.two Like [T2].[two]) ' match column two; the "is null" is needed in case the value is not there in T1
AND (T1.three Is Null Or T1.three Like [T2].[three]) ' match column three; the "is null" is needed in case the value is not there in T1
这导致下面的 table。它部分有效,但 returns 行它不应该(在下面标记)。
+--------+--------+--------+--------+----------+----------+----+
| Result |
+--------+--------+--------+--------+----------+----------+----+
| T1.one | T2.one | T1.two | T2.two | T1.three | T2.three | cat|
+--------+--------+--------+--------+----------+----------+----+
| aaaa | aaaa | 1111 | * | | * | 1 |
| aaaa | * | 1111 | * | | test | 3 | -> THIS SHOULD NOT BE RETURNED
| bbbb | * | 2222 | 2* | | * | 2 |
| bbbb | * | 2222 | * | | test | 3 | -> THIS SHOULD NOT BE RETURNED
| cccc | * | | 2* | test | * | 2 | -> THIS SHOULD NOT BE RETURNED
| cccc | * | | * | test | test | 3 |
| dddd | * | | 2* | | * | 2 | -> THIS SHOULD NOT BE RETURNED
| dddd | * | | * | | test | 3 | -> THIS SHOULD NOT BE RETURNED
+--------+--------+--------+--------+----------+----------+----+
我已经开始这个工作几个小时了,但我不知道如何做我需要的。
我认为这不是特定于数据库的问题,但如果它很重要,我正在尝试使用 MS Access 2013 来完成此操作。
SELECT T1.one, T2.one, T1.two, T2.two, T1.three, T2.three, T2.id
FROM T2 LEFT JOIN T1
on t1.one = t2.one and t1.two = t2. two and t1.three = t2.three
除非指定模式,否则无法使用 like
比较列。
SELECT T1.one, T2.one, T1.two, T2.two, T1.three, T2.three, T2.ID
FROM T1, T2
WHERE (t1.one LIKE t2.one + '*'
AND t1.two LIKE t2.two + '*'
AND t1.three LIKE t2.three + '*')
OR t1.one LIKE t2.one + '*'
OR t1.two LIKE t2.two + '*'
OR t1.three LIKE t2.three + '*'
据我所知,您有 2 个问题:无法在计算字段上联接表。
-> T1 中第二列以 2 开头的所有行
-> T1 中第二列以 3 开头且第三列等于 hi
的所有行
不能这样做。
但是,我建议您使用 sql fiddle 来展示您的示例。
我已经帮你做了。
为了将来参考,我想我找到了一个可行的答案:http://sqlfiddle.com/#!9/595eb/1。
我没有在 T2
中使用 *
(通配符),而是使用 null
,然后在查询中检查值是否为 null
。这似乎有 desired/expected 结果。
WHERE
(T2.one is null or T1.one = T2.one) AND
(T2.two is null or T1.two like T2.two) AND
(T2.three is null or T1.three like T2.three);
如果 T2
中的多行与 T1
中的一行匹配,将会出现重复,所以我仍在努力解决这个问题。
我有两个 table:
T1
是一个table的数据- 列
one
不能有null
- 列
two
和three
可以有null
s
- 列
T2
是一个table的分类规则- 它具有与
T1
相同的列以及cat
列来表示类别 - 想法是前三列具有用于确定
T1
中的行应该如何分类以及是否应该分类的标准 T2
中的一行可能有 2+ 列的值,这意味着T1
中有多个条件需要匹配(例如T1.two like "2*" and T1.three like "hi"
)
- 它具有与
我想要一个查询来查找 T1
中根据 T2
中的条件匹配的行。这是一个例子:
+------+------+-------+
| T1 |
+------+------+-------+
| one | two | three |
+------+------+-------+
| aaaa | 1111 | |
| bbbb | 2222 | |
| cccc | | test |
| dddd | | |
+------+------+-------+
+------+-----+-------+------+
| T2 |
+------+-----+-------+------+
| one | two | three | cat |
+------+-----+-------+------+
| aaaa | * | * | 1 | -> all rows in T1 where column one equals aaaa
| * | 2* | * | 2 | -> all rows in T1 where column two starts with 2
| * | * | test | 3 | -> all rows in T1 where column three equals test
| * | 3* | hi | 3 | -> all rows in T1 where column two starts with 3 AND column 3 equals hi
+------+-----+-------+------+
我在 T2
中得到了 *
,因为我想说这些列中的值应该无关紧要。因此,以第二行为例,我说的是匹配 T1 中的所有行,其中:
one
是什么two
以 2 开头
three
随便什么
我的想法是进行不明确的连接,并在匹配的行上进行过滤:
SELECT T1.one, T2.one, T1.two, T2.two, T1.three, T2.three, T2.id
FROM T1, T2
WHERE
(T1.one Like [T2].[one]) ' match column one
AND (T1.two Is Null Or T1.two Like [T2].[two]) ' match column two; the "is null" is needed in case the value is not there in T1
AND (T1.three Is Null Or T1.three Like [T2].[three]) ' match column three; the "is null" is needed in case the value is not there in T1
这导致下面的 table。它部分有效,但 returns 行它不应该(在下面标记)。
+--------+--------+--------+--------+----------+----------+----+
| Result |
+--------+--------+--------+--------+----------+----------+----+
| T1.one | T2.one | T1.two | T2.two | T1.three | T2.three | cat|
+--------+--------+--------+--------+----------+----------+----+
| aaaa | aaaa | 1111 | * | | * | 1 |
| aaaa | * | 1111 | * | | test | 3 | -> THIS SHOULD NOT BE RETURNED
| bbbb | * | 2222 | 2* | | * | 2 |
| bbbb | * | 2222 | * | | test | 3 | -> THIS SHOULD NOT BE RETURNED
| cccc | * | | 2* | test | * | 2 | -> THIS SHOULD NOT BE RETURNED
| cccc | * | | * | test | test | 3 |
| dddd | * | | 2* | | * | 2 | -> THIS SHOULD NOT BE RETURNED
| dddd | * | | * | | test | 3 | -> THIS SHOULD NOT BE RETURNED
+--------+--------+--------+--------+----------+----------+----+
我已经开始这个工作几个小时了,但我不知道如何做我需要的。
我认为这不是特定于数据库的问题,但如果它很重要,我正在尝试使用 MS Access 2013 来完成此操作。
SELECT T1.one, T2.one, T1.two, T2.two, T1.three, T2.three, T2.id
FROM T2 LEFT JOIN T1
on t1.one = t2.one and t1.two = t2. two and t1.three = t2.three
除非指定模式,否则无法使用 like
比较列。
SELECT T1.one, T2.one, T1.two, T2.two, T1.three, T2.three, T2.ID
FROM T1, T2
WHERE (t1.one LIKE t2.one + '*'
AND t1.two LIKE t2.two + '*'
AND t1.three LIKE t2.three + '*')
OR t1.one LIKE t2.one + '*'
OR t1.two LIKE t2.two + '*'
OR t1.three LIKE t2.three + '*'
据我所知,您有 2 个问题:无法在计算字段上联接表。
-> T1 中第二列以 2 开头的所有行 -> T1 中第二列以 3 开头且第三列等于 hi
的所有行不能这样做。
但是,我建议您使用 sql fiddle 来展示您的示例。
我已经帮你做了。
为了将来参考,我想我找到了一个可行的答案:http://sqlfiddle.com/#!9/595eb/1。
我没有在 T2
中使用 *
(通配符),而是使用 null
,然后在查询中检查值是否为 null
。这似乎有 desired/expected 结果。
WHERE
(T2.one is null or T1.one = T2.one) AND
(T2.two is null or T1.two like T2.two) AND
(T2.three is null or T1.three like T2.three);
如果 T2
中的多行与 T1
中的一行匹配,将会出现重复,所以我仍在努力解决这个问题。