Match domain and extension of two email addresses in MySQL? - mysql

I have two tables with email addresses. Some emails have sub-domains, and others don't. I need to LEFT OUTER JOIN these tables based on the domain name and extension, while ignoring any sub-domains and anything left of the # symbol.
Contacts.Contact
Managers.Manager
bob#api.ibm.com
sarah#ibm.com
jim#sales.ibm.com
jane#mgt.yahoo.ca
joe#ibm.com
fred#google.com
zoe#yahoo.com
sam#facebook.co.uk
elf#api.yahoo.ca
frank#yahoo.ca
jack#oracle.com
SELECT Contact.Contact,
Managers.Manager
FROM Contact
LEFT OUTER JOIN Manager ON ???
should yield the following results:
Field: Contact
Field: Manager
bob#api.ibm.com
sarah#ibm.com
jim#sales.ibm.com
sarah#ibm.com
joe#ibm.com
sarah#ibm.com
zoe#yahoo.com
elf#api.yahoo.ca
jane#mgt.yahoo.ca
frank#yahoo.ca
jane#mgt.yahoo.ca
jack#oracle.com
The ibm.com and yahoo.ca emails were matched. So basically, the pattern is: IGNORE#IF_THIS_EXISTS_THEN_IGNORE.MATCH.MATCH Is this possible? If it is, then what is the correct SELECT statement to yield these results? Thanks.

To extract the (at most) two domain parts on the right, just do:
substring_index(substring_index(email,'#',-1),'.',-2)
Apply that to both Contact and Manager and join on the results being equal.

Related

How do I get all strings that do not contain another string in MySQL?

I have a table called "Domains" with field "Name" (unique, always lowercase) which contains a list of domains and subdomains on my server like:
blah.example.com
www.example.com
www.blah.example.com
example.com
example.nl
example.org
Looking at this list, names 1, 2 and 3 are subdomains of item 4. And I'm looking to just find all domains in this table without these subdomains. Or, to be more precise, any name that does not have part of it in the name from another record. Thus only item 4, 5 and 6.
If record 4 was missing then this query would also have item 1 and 2 as result, but not item 3. After all, item 3 has item 1 as part of it.
Just trying to find the query that can provide me this result... Something with select d.name from domains where d.name not in... Well, there my mind goes blank.
Why?
This list of domains is generated by my web server which registers every new domain that gets requested on it. I'm working on a reporting page where I would display the top domain names to see if there are any weird domains in it. For some reason, I sometimes see unknown domain names in these requests and this might give some additional insight in it all.
I am going to change my code so it will include references to parent domains in the same table in the future but for now I'll have to deal with this and a simple SQL solution.
Use a self-join that matches on suffixes using LIKE
SELECT d1.name
FROM domains AS d1
LEFT JOIN domains AS d2 ON d1.name LIKE CONCAT('%.', d2.name)
WHERE d2.name IS NULL
DEMO

SQL Is this Outer or Inner Join and how to join on Array

Seemed simple when I started and have done this before, now I confused myself and at a road block.
Have two tables: News_Table and a People_Table. Under the News_Table there is a field: News_People_Contributed and it has the ID's of the People_Table in array format (1,4,7,10) thus Four People contributed. I am creating a search parameter that looks up News_Header AND News_People_Contributed and can't figure how to create the search column.
News_Table
News_ID
News_Header
News_People_Contributed
People_Table
People_ID
People_First_Name...
Is it something like...
Select*
From News_Table
Left Join News_Table
On People_Table.People_ID IN (News_Table.News_People_Contributed)
Where Search_Param Like '%News_Header%' OR Search_Param Like '%People_First_Name%'
The problem is (News_Table.News_People_Contributed) is a string and the ID's are not. Plus I may not have people contributed etc. To make the issue even more complex, I'm doing this in MS Access instead of MySql, so have to code it "old school" sql for work around.
Perform a cross join and filter on matches in the string list. It says nothing about efficiency or form (as already commented on), but it works.
SELECT *
FROM News_Table, People_Table
WHERE InStr([News_People_Contributed],CStr([People_ID])) > 0;
This only answers part of the problem: The join -- the issue everyone seemed concerned about in the initial comments. There are not enough details about about the Search_Parameter to provide help on that. Supply more detail if you need more help there.

Inner join inverse Php mysql

I have two tables master_domain_list and temporary_domain_list. master_domain_list contains a list with columns for the email ID and domain name. temporary_domain_list also contains the domain column.
I want to filter temporary_domain_list to show all domain names which are not present in master_domain_list.
I tried the following code (inner join inverse) but it's giving the whole list of domains from temporary_domain_list without removing the domains in master_domain_list.
$result1=mysqli_query($cons, "
SELECT domain
FROM temporary_domain_list
WHERE domain NOT IN (SELECT domain
FROM master_domain_list)
");

Access 'No Records Found' when Records Exist

I have a form in Access 2010 that's used as a search form to filter records matching specific criteria.
I transferred information in the backend from one set of tables to another. Now, the filter doesn't work. Even if I leave all the criteria blank - ie. set it to bring up all records - it tells me, 'No records found.'
I've remapped the tables a few times, made sure they all have information, and are linking and opening properly. What could be preventing Access from finding the records?
Here's the filter query, if it helps any. It doesn't appear to be filtering properly, even though it works fine with the old tables.
SELECT Activity.*, ActivityCash.*, EngSchDates.*, Monitoring.*, Procurement.*,
LookupDistrict.*
FROM ((((Activity LEFT JOIN LookupDistrict ON Activity.District =
LookupDistrict.District) INNER JOIN ActivityCash ON Activity.GWP = ActivityCash.GWP)
INNER JOIN EngSchDates ON Activity.GWP = EngSchDates.GWP)
INNER JOIN Procurement ON Activity.GWP = Procurement.GWP) INNER JOIN Monitoring ON
Activity.GWP = Monitoring.GWP ORDER BY Activity.District,
Activity.[ProgramYear], [Activity].GWP;
In general, to debug these types of problems, try removing one table at a time from the FROM clause (and SELECT) until you get your results back.
Remove AND [Activity].[Designer] like '*' from the query.

MySQL with two Joins and a Where clause

I have 3 tables I want to join.
OFFICE contains address/contact details for an office.
CRIMECAT contains categories of crime law that an office may deal with and is related to the OFFICE table via the 'f_id'
CIVILCAT contains categories of civil law that an office may deal with and is related to the OFFICE table via the 'f_id' as well.
An office may deal with categories of crime law, civil law, both or none.
The user puts in a location and also decides via a range of checkboxes which areas of law they're interested in before hitting search. This should then return a list of the addresses of offices that deal with any of the checked categories in that location.
This works perfectly for either a crime category or a civil category, but as soon as one or more of each is selected the query returns zero results.
The working style of query is as follows:
SELECT DISTINCT OF.f_id, OF.acc, OF.add1, OF.add2, OF.add3, OF.city, OF.pc, OF.tel
FROM office OF
JOIN crimecat CR ON OF.f_id=CR.f_id
WHERE OF.id ='3946' AND CR.cat = 'crm'
The query I'm banging my head against a brick wall on is:
SELECT DISTINCT OF.f_id, OF.acc, OF.add1, OF.add2, OF.add3, OF.city, OF.pc, OF.tel
FROM office OF
JOIN crimecat CR ON OF.f_id=CR.f_id
JOIN civilcat CI ON OF.f_id=CI.f_id
WHERE OF.id ='3946' AND ((CR.cat = 'crm') OR (CI.cat = 'aap'))
I've also tried using a variant of the WHERE clause which also returns zero:
WHERE (OF.id ='3946' AND CR.cat = 'crm') OR (OF.id ='3946' AND CI.cat = 'aap')
I'm beginning to think the issue is with the JOIN(s) rather than the WHERE clause but can't think of a better way of writing them.
something like this may help. i suspect you are looking for either , but you are requesting both
SELECT DISTINCT OF.f_id, OF.acc, OF.add1, OF.add2, OF.add3, OF.city, OF.pc, OF.tel
FROM office OF
LEFT JOIN crimecat CR ON OF.f_id=CR.f_id
LEFT JOIN civilcat CI ON OF.f_id=CI.f_id
WHERE OF.id ='3946' AND ((CR.cat = 'crm') OR (CI.cat = 'aap'))