I have 2 tables, Table1 with base codes (ex. MC890) and Table2 with multi level codes (MC890.1.2.3) surrounded by corresponding data. I need a select query to pull all the records from Tabel2 that match MC890 regardless of multiple levels (MC890.*.*.*). I have seen variations of the Like function, but I haven't been able to get anything to work. Please help.
Select query for partial matches between two tables
Collapse
X
-
If you already know what base code you will be searching for in your Table 2, there is no need to join or do a nested select. You would be able to do something like:
Code:SELECT * FROM Table2 WHERE Table2.MLC LIKE 'MC890*'
-
It's a little difficult to communicate this without knowing your exact Table Names and Field Names, but hopefully this gives you an idea:
Code:SELECT Table1.BaseCode, Table2.* FROM Table1 LEFT JOIN Table2 ON Table2.FullCode LIKE Table1.BaseCode & '*'
Comment
-
Have you looked at Database Normalization and Table Structures?
Storing the base code in one table is a start. You then relate the tiers back to this table; however, the exact design is outside of this thread.Comment
-
Indeed. This looks like Table two should have a Compound Index of multiple fields. That way the database system knows that the first part of the index is supposed to match the BaseCode of Table1.
The way you have it now will work, even though it won't show up in an Access QueryDef except in SQL view, but it will not get much benefit from your indexing - which can be extremely important of course.Comment
Comment