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.
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
I am building a training platform for work. I have created the requirements for a user to be trained based on a role given to them. If that role is aligned to a document it will sit against the user. I have managed to get most of the way but am struglling on the best way to finish the where statement within mysqli.
tbldocfiles is a list of my files. I am looking at docid (could be multiple files associated to the document)
tbltrainingaccess sets the roles (driver, warehouseman, customer services) and shows which role (by id) is associated to the document in docfiles.
tblusertraining is the list of users and what role they have associated to them. (driver, warehouseman, customer services).
I am listing the documents associated to the user so have thought the following is the best way:
Look at the user and how many roles he/she is allocated
Look at the roles returned in point 1 (where function)
Identify and match the documents that have the same roles as the user (Join function)
create the list, then look at the unique values for docid. (distinct value)
Example User Bri has the driver and warehouseman role.
There are 5 documents in the db, 3 of them are associated to the driver role (docid 1,2,3) and 2 of them are associated to the warehouseman role (docid 2,4) the 5th document is associayted to customerservice.
My query should do this:
List all documents associated to the roles, that are associated to the user Bri
1
2
3
2
4
Now select unique values (using docid) from the above list:
1,2,3,4.
So my answer will be a used as a count function at the end using mysql_fetch_rows
SELECT DISTINCT tbldocfiles.docid FROM tbldocfiles LEFT JOIN tbltrainingaccess ON (tbldocfiles.docid = tbltrainingaccess.docid) where groupid='1' or groupid='9'
The above code works. but i've got myself confused.
The where statement needs to be the result of a query similar to :
select * from tblusertrainingrole where userid='1' (1 will be a variable based on page selection)
the result in this would be 1, 9 which are the groupid results.
Basically any help would be appreciated! I am sure it will be simple but have burnt myself out on this for a while and most answers in here helped with joining but not the where statement (that I could find)
Thank you in advance everyone!
You can do a select statement in the where. Since it is an or statement you can use in for the results. Please replace * with the column name for the value you need. Should look like
where groupid in (select * from tblusertrainingrole where userid = '1')
I have customer_data.csv as follows:
first_name,last_name,cust_no
Test,User1,12345
Test,User2,99999
My address_Details.csv is as follows:
addr_type,line1,line2,line3,cust_no
work,x,y,z,12345
school,a,b,c,12345
Homehome, ,m, ,n, ,o, ,12345
work,1,2,3,99999
My final output should be as follows:
first_name,last_name,cust_no,no_of_addrs,add_type,line1,line2,line3
test,User1,12345,3,work,x,y,y,school,a,b,c,home,m,n,o
test,User2,99999,1,work,1,2,3,,,,,,,,
Where each id has the number of entries joined together?
I used map
I have gotten the following result:
Test|User1|12345|work|x|y|z
Test|User1|12345|school|a|b|c
Test|User1|12345|home|m|n|o
Test|User2|99999|work|1|2|3
What component must I use instead?
1st: the main must be customer_data.csv and the lookup must be address_Details.csv.
2nd: if you want to reject customer without any address, in the tMap, click on the wrench in the lookup table then select "inner join" and on the outpu table, click the wrench and select "catch lookup inner join reject" to True (False is the default).
Regards,
TRF
I am trying to create a form using Rails Admin, version 4.0.0. For this form I have tables called items, locations, cities and item_in_city. Tables are related in the following ways:
items table has a column called location which is array or location ids where item is present
location table has a column called city_id which connects it to city table
there is item_in_city table which has item_id and its corresponding list of city_id
Now, I want to create a form using Rails Admin, where I can give option to add a new item. While adding this new item I want to give an option to select (multiple) cities and corresponding to cities I want to give list of locations which can be selected.
I figured out the solution, So I thought of posting it as it can be useful to others. Combination of city name and location can be posted by defining an enum method in the following way
def location_enum
Location.all.collect {|l| [ l.city.name + ' ' + l.name, l.id] }.sort
end