Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select rows which contains part of values from another list of rows?

Tags:

c#

sql

firebird

Its in firebird + c# application. I have table like:

TableA:
        30RTabc1 someothervalue1 someothervalue2
        30RTabc2 someothervalue3 someothervalue4
        30RTabc1 someothervalue5 someothervalue6
        30RTabc4 someothervalue7 someothervalue8
        30RTabc1 someothervalue9 someothervalue10

TableB:
            abc1 someothervalue1 someothervalue2
            abc2 someothervalue3 someothervalue4
            abc3 someothervalue5 someothervalue6

So in result i would have joint TableC which would have values from TableA where first column contains values from TableB first column so it would look like this:

Result of joining TableA + TableB where column1 string of TableA contains string from column1 TableB:

TableC:
30RTabc1 someothervalue1 someothervalue2
30RTabc2 someothervalue3 someothervalue4
30RTabc1 someothervalue5 someothervalue6
30RTabc1 someothervalue9 someothervalue10

What is best way to do it, in firebird some sql query or in app, c# - loop? (The key is column1, other values can be completely different, someothervalue is only example, all can be random)

like image 544
user3762355 Avatar asked Feb 03 '26 14:02

user3762355


2 Answers

If it is easy to do in SQL, I see no reason to do it in c#, you would have to fetch a lot of data just to discard it afterwards.

As to how to do it in SQL, it depends in the '30RT' part can change or not :

If not :

SELECT t1.*
FROM TableA t1
INNER JOIN TableB t2
ON t1.Key = '30RT'+t2.Key

If it can be anything :

SELECT t1.*
FROM TableA t1
INNER JOIN TableB t2
ON t1.Key like '%'+t2.Key
like image 176
Ndech Avatar answered Feb 05 '26 03:02

Ndech


As a rule of thumb: Delegate jobs to experts. In this case: Let the Firebird do the job with a a simple JOIN because she is the expert when it comes to table manipulation, combination, searching etc.

However, if the tables are in memmory anyway, you might consider LINQ to join the tables.

like image 22
alzaimar Avatar answered Feb 05 '26 03:02

alzaimar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!