Forum Moderators: open
I have a table with two columns of information:
---------------------------
Column A ¦ Column B
---------------------------
example.net ¦ websiteA.com
example.org ¦ websiteA.com
example.net ¦ websiteB.com
example.org ¦ websiteD.org
example.net ¦ websiteE.net
example.org ¦ websiteE.net
example.net ¦ websiteF.edu
---------------------------
If I input 'example.net' and 'example.org' I want the query to return results from column B where both are present in column A. For example, if I input example.net and example.org against the above data, it returns:
websiteA.com
websiteE.net
Ive tried to develop a query statement for this but nothing I try seems to work. Any ideas?
**This might be what you want... the issue would be if you added a 3rd search term you would have to change the "HAVING" clause to be 3... like this:
having
count(coulmnB) = 3
I am not sure if this is 100% right but post back and we will see if we can get it.
Basically what is happening in this query is we are counting the amount of identical coulmnBs that are being returned then telling the query we only want them if it has a count of 2... hopefully indicating that it matched both search terms.
If you have data like this you will get a bad result
example.net ¦ websiteA.com
example.ca ¦ websiteA.com
example.net ¦ websiteA.com
If you input 'example.net' and 'example.org' then "websiteA.com" would still be returned because it matched 'example.net' 2 times but not example.org... this would be a false positive if I understand this correctly. Could the data appear this way?
select
'term1' as term_match, columnB
from
table_name
where
columnA = $serch_term1
UNION
select
'term2' as term_match, columnB
from
table_name
where
columnA = $serch_term2
Then looping through the results check for columnB values from term1 that match columnB values from term2, discard anything else that doesn't match and keep what does.
A little more laborious but it will be more logically sound.
This will show you how many matches their are for example.net and example.org
you will have to loop through the results and filter the results that have a counter of less than 2
You could put 20 search terms inside the IN statement e.g in("example.net","example.org","example.org","example.org") and so on.....
then just filter out the results returned that are less that the amount you are searching for