在Table1中查找与Table2中的条件行匹配的行

时间:2015-07-22 17:24:19

标签: sql ms-access ms-access-2013

我有两张桌子:

  1. T1是一个数据表
    • one不能拥有null
    • twothree可以有null s
  2. T2是分类规则表
    • 它与T1具有相同的列以及用于表示类别的cat
    • 我们的想法是,前三列的标准用于确定T1中的行应该如何以及是否应该归类
    • T2中的行可能包含2 +列中的值,这意味着需要在T1中匹配多个条件(例如T1.two like "2*" and T1.three like "hi"
  3. 我想要一个查询,根据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
    

    结果如下表所示。它部分有效,但返回它不应该的行(标记如下)。

    +--------+--------+--------+--------+----------+----------+----+
    | 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执行此操作。

4 个答案:

答案 0 :(得分:1)

从我看到的,你有2个问题:你不能在计算字段上连接表。

- > T1中所有行,其中第二列以2开头 - > T1中所有行,其中第二列以3开头,第3列等于hi

不能这样做。

但是,我建议您使用sql小提示来显示您的示例。

我已经为你完成了。

http://sqlfiddle.com/#!9/bb1fc/2

答案 1 :(得分:0)

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比较列。

答案 2 :(得分:0)

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 + '*'

答案 3 :(得分:0)

为了将来参考,我想我找到了一个有效的答案:http://sqlfiddle.com/#!9/595eb/1

我没有在return PartialView("_PriorityTasks", viewModel); 中使用*(通配符),而是使用T2,然后在查询中检查值是否为null。这似乎有预期/预期的结果。

null

如果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中的一行,则会有重复项,因此我仍然在努力解决这个问题。