sql - find rows in Table1 that match criteria rows in Table2 -
i've got 2 tables:
t1
table of data- column
one
cannot havenull
- column
two
,three
can havenull
s
- column
t2
table of categorization rules- it has same columns
t1
alongcat
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 int1
(e.g.t1.two "2*" , t1.three "hi"
)
- it has same columns
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
anythingtwo
starts 2three
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.
Comments
Post a Comment