sql - find rows in Table1 that match criteria rows in Table2 -


i've got 2 tables:

  1. t1 table of data
    • column one cannot have null
    • column two , three can have nulls
  2. t2 table of categorization rules
    • it has same columns t1 along cat column represent category
    • the idea first 3 columns have criteria used determine how , if rows in t1 should categorized
    • it possible row in t2 have values in 2+ columns meaning there multiple criteria need match in t1 (e.g. t1.two "2*" , t1.three "hi")

i want query finds rows in t1 match based on criteria in t2. here example:

+------+------+-------+ | t1                  | +------+------+-------+ | 1  | 2  | 3 | +------+------+-------+ | aaaa | 1111 |       | | bbbb | 2222 |       | | cccc |      | test  | | dddd |      |       | +------+------+-------+  +------+-----+-------+------+ | t2                        | +------+-----+-------+------+ | 1  | 2 | 3 | cat  | +------+-----+-------+------+ | aaaa | *   | *     |    1 | -> rows in t1 column 1 equals aaaa | *    | 2*  | *     |    2 | -> rows in t1 column 2 starts 2 | *    | *   | test  |    3 | -> rows in t1 column 3 equals test | *    | 3*  | hi    |    3 | -> rows in t1 column 2 starts 3 , column 3 equals hi +------+-----+-------+------+ 

i've got * in t2 because trying value in columns should not matter. using second row example i'm saying match rows in t1 where:

  • one anything
  • two starts 2
  • three anything

my thought ambiguous join , filter on matching rows:

select t1.one, t2.one, t1.two, t2.two, t1.three, t2.three, t2.id t1, t2     (t1.one [t2].[one])                             ' match column 1     , (t1.two null or t1.two [t2].[two])       ' match column two; "is null" needed in case value not there in t1     , (t1.three null or t1.three [t2].[three]) ' match column three; "is null" needed in case value not there in t1 

this results in table below. partially works returns rows should not (marked below).

+--------+--------+--------+--------+----------+----------+----+ | result                                                       | +--------+--------+--------+--------+----------+----------+----+ | t1.one | t2.one | t1.two | t2.two | t1.three | t2.three | cat| +--------+--------+--------+--------+----------+----------+----+ | aaaa   | aaaa   |   1111 | *      |          | *        |  1 |  | aaaa   | *      |   1111 | *      |          | test     |  3 | -> should not returned | bbbb   | *      |   2222 | 2*     |          | *        |  2 |  | bbbb   | *      |   2222 | *      |          | test     |  3 | -> should not returned | cccc   | *      |        | 2*     | test     | *        |  2 | -> should not returned | cccc   | *      |        | *      | test     | test     |  3 |  | dddd   | *      |        | 2*     |          | *        |  2 | -> should not returned | dddd   | *      |        | *      |          | test     |  3 | -> should not returned +--------+--------+--------+--------+----------+----------+----+ 

i've started @ few hours cannot figure out how need.

i figure not database-specific question if matters i'm trying ms access 2013.

from see, have 2 problems: cannot join tables on computed fields.

-> rows in t1 column 2 starts 2 -> rows in t1 column 2 starts 3 , column 3 equals hi

cannot done way.

however, suggest use sql fiddle show example.

i have done you.

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


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -