Using LIKE in a JOIN query - sql

I have two separate data tables.
This is Table1:
Customer Name Address 1 City State Zip
ACME COMPANY 1 Street Road Maspeth NY 11777
This is Table2:
Customer Active Account New Contact
ACME Y John Smith
I am running a query using the JOIN where only include rows where the joined fields from both tables are equal.
I am joining Customer Name from Table1 and Customer from Table2. Obviously no match. What I am trying to do is show results where the first 4 characters match in each table so I get a result or match. Is this possible using LIKE or LEFT?

Yes, that's possible.
But I doubt, that every name in table 2 only has 4 letters, so here's a solution where the name in table2 is the beginning of the name in table1.
Concat the string with a %. It's a placeholder/wildcard for "anything or nothing".
SELECT
*
FROM
Table1
INNER JOIN Table2 ON Table1.CustomerName LIKE CONCAT(Table2.Customer, '%');
Concatenating of strings may work differently between DBMS.

It probably is, though this might depend on the Database you are using. For example, in Microsoft SQL, it would work to use somthing like this:
SELECT *
FROM [Table1] INNER JOIN [Table2]
ON LEFT([Table1].[Customer Name],4) = LEFT([Table2].[Customer],4)
Syntax may be different if using other RDBMS. What are you trying this on?

Seems like this should work:
Select *
From Table1, Table2
Where Table1.CustomerName Like Cat('%',Trim(Table2.CustomerName),'%')

If you are only trying to match first four Characters you can use following :
SELECT --your columns
FROM Table1 T1
JOIN Table T2
ON
SUBSTRING ( T1.CustomerName ,1, 4) = SUBSTRING ( T2.Customer ,1, 4)

Related

Fuzzy match column to column

I'm trying to find a way to match a column of clean data in table 1 to a column of dirty data in table2 without making any changes to the dirty data. I was thinking a fuzzy match, but there are too many entries in the clean table to allow for CDEs to be used. So, for example:
Table 1
GroupID CompanyName
123 CompanyA
445 CompanyB
556 CompanyC
Table 2
GroupID Patientname
AE123789 PatientA
123987 PatientB
445111 PatientC
And I'm trying to match the insurance company to the patient using the group number. Is there a matching method out there? (Fortunately the group numbers are actually much longer and when looking for a single group's worth of patients, fuzzy matching works really well, so they seem to be unique enough to be applied here).
Working in SQL server 2008.
This changes slightly depending on which database you are using, but it looks like you're looking for something like this:
MSSQL
select *
from table1 t1
join table2 t2 on t2.groupid like '%'+cast(t1.groupid as varchar(max))+'%'
SQL Fiddle Demo
MySQL - use Concat():
select *
from table1 t1
join table2 t2 on t2.groupid like concat('%',t1.groupid,'%')

Join over substring across tables in MSSQL

I have two tables with one table with one column having a URL and another table having a substring from that URL
Table 1
Id | URL
----------
1 ...\aaa_common\
2 ...\aaa_qa..
3 ...\aaa_test\Analytics
Table 2
SomeId | compname
-----------------
1 aaa_common
2 aaa_qa
3 aaa_test
It is possible to join using string functions (charindex and substring) . But is there an easier alternative?
Yes you can use join, but not sure that this is best method, cause join on string is not good idea, also I am not sure about repetitive values in your table. Still if require to do so, I will suggest you to have one more column in your Table1, in which you can update only compname from same table using sub-string & then join both tables including new column from Table1 & compname from Table2.
Also for using sub-string you should be 100% sure with index/pattern of your compname in string of Table1.
Please look into this DEMO
Just has example of join on string using sub-string & charindex
You can join using like, but it will be a bit of a performance hit.
select
*
from
table_1 t1
inner join table_2 t2 on
t1.url like concat('%',t2.compname, '%')

SQL Join for cell content, not column name

I read up on SQL Join but as far as I understand it, you can only join tables which have a column name in common.
I have information in two different tables, but the column name is different in each. I need to pull information on something which is only in one of the tables, but also need information from the other. So was looking to join/merge them.
Here is what I mean..
TABLE1:
http://postimg.org/image/hnd63c2f5/
The cell content 18599 in column from_pin_id also pertains to content in another table:
TABLE2:
http://postimg.org/image/apmu26l5z/
My question is how do I merge the two table details so that it recognizes 18599 is referring to the same thing, so that I can pull content on it from other columns in TABLE2?
I've looked through the codes on W3 but cannot find anything to what I need, as mentioned above, it seems to be just for joining tables with a common column:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
You can write as :
select * from table1
where from_pin_id in
(
select from_pin_id
from table1
intersect
select id
from table2
)
Intersect operator selects all elements that belong to both of the sets.
Change the table names and the columns that you select as needed.
SELECT table1.id, table1.owner_user_id, table1.from_pin_id, table2.board_id
FROM table1
JOIN table2 ON table1.from_pin_id = table2.id
GROUP BY id, owner_user_id, from_pin_id, board_id

Joining SQL queries with where clause

I have 2 tables in a MYSQL database wich look like this:
Klant:
ID Naam Email Soort Status
6 test test test2
7 status test test test 20
8 soort test test test
9 soort test 2 test2 Museum
Mail:
ID Content Datum Titel
1 (lots of encoded HTML) 18-03-13 test
2 (lots of encoded HTML) 18-03-13 test2
4 (lots of encoded HTML) 18-03-13 alles weer testen
(yes, I'm still testing alot^^)
Now I have a SQL query that selects all from 'Klant' with a where clause which gets the ID from a previous page:
$strSQL = "SELECT * FROM Klant WHERE ID = '".$_GET["ID"]."' ";
What I want is to JOIN this query with the following query:
SELECT ID, Titel FROM Mail;
EDIT:
From all your answers and comments I think I begin to think my question maybe is totally wrong.. I'll explain where I need it for and I might not even need JOIN? I currently have a table wich includes the data from 'Klant' which looks like this:
The meaning is that I add another table which includes all the ID's and Title's from 'Mail'. I am sorry for the confusion I may have caused with you since I wasn't that clear with my question. I hope that this may clear up what I want and you guys can maybe tell me if I even need to JOIN this or can I do something else?
I am still a student and this is the first time I've had to use JOIN and I can't figure this out. If anyone can show me how to do this or push me in the right direction it would be great!
SELECT * FROM Klant t1
JOIN
SELECT ID, Titel FROM Mail t2
ON t1.ID = t2.ID
WHERE t1.Name = 'test'
To have the desired result do the following:
SELECT * FROM Klant t1
JOIN
SELECT ID, Titel FROM Mail t2
ON t1.ID = t2.ID
And if you want to have a specific row than just add the where clause:
WHERE t1.ID = 6
or
WHERE t1.Naam = 'test'
and so on
It is difficult to see how a JOIN is applicable in the example in your question.
A JOIN let's you pull information from more than one table based on a relationship. As far as I can see, your table's don't have any way to link a row in one with a row in the other, unless SteveP is correct and your id's provide that relationship.
For example, if your klant table had a mail_id column then you could do
SELECT *
FROM klant
JOIN mail ON klant.mail_id = mail.id
and this would return a row for every matching pair of rows in the two tables. Alternatively you could use a LEFT OUTER JOIN to pull back all rows from the table on the left of the JOIN and optionally data from a matching row on the right.
If there is nothing joining the table, you can use a CROSS JOIN which will return you a full cartesian of each row in table1 with every row in table2.
Something people often confuse with a JOIN is a UNION which allows you to write 2 SELECT statements and return the result set of both combined/joined together, but these should return the same columns in each query (e.g. selecting NULL in place of the column in a query if the query doesn't pull data for that column)
I'm guess that you want to join on the ID field which is common between the tables.
select * from Klant, Mail where Klant.ID = '".$_GET["ID"]."' and Klant.ID = Mail.ID
You can also do
select * from Klant
join Mail on Mail.ID = Klant.ID
where Klant.ID = '".$_GET["ID"]."'
You can do this directly by using the following query :
select k.ID,k.Naam, k.Email,k.Soort,k.Status, m.ID,m.Titel from Klant k, Mail m where k.ID = m.ID and k.ID = '".$_GET["ID"]."'

Join on ((= Statement) or ((!= Statement), other stipulations))

I have some information where the IdNumbers (not primary key Ids, just random Ids assigned to individuals) are not always correct in my first table.
Therefore I am joining my second table on both Ids and names, and trying to get it to where it will join on names only if the IdNumbers do not match.
I'm working on a query with a join statement that is roughly as follows (I'm leaving out the SELECT, WHERE, and ORDER BY sections because I believe that they are not having an effect on this issue and I don't want to be confusing, as they are stupidly complex - if the portion of the query below should be working like I want it to and the problem is obviously somewhere else, then just tell me so and that will answer my question):
FROM Table1
FULL OUTER JOIN Table2 ON ((Table1.IdNumber = Table2.IdNumber)
OR (Table1.IdNumber != Table2.IdNumber
AND Table1.Lname = Table2.Lname
AND Table1.Fname = Table2.Fname))
However, it is joining the people who have both matching Ids and matching names multiple times like so:
Fname M Lname Table1.IdNumber Table2.IdNumber2
Matthew - Smith 1 2
Matthew H Smith 2 1
Matthew - Smith 1 1
Matthew H Smith 2 2
So it is pulling the last 2 because their ids match, but also joining the first 2 because their ids do not match and their names match, but why is it even joining the first 2 to begin with? I suspect that it ignores the != statement when deciding where to join since the other conditions are fulfilled, but I'd like it to take this != statement into account somehow.
If this should be working, like I said before, just tell me and it will answer my question.
(*EDIT)
Sorry, I should have named these properly - I've revised the names. And the full outer join is necessary, I need everything from both tables no matter what and it's working fine, but thank you for the suggestion.
Given how messy this would be to do in one JOIN, I would suggest using a temp table to hold the relationships.
You can insert all of your IDs from the into first table into a temp table, then do two passes to update a column holding the 2nd table ID - first using where the ID matches, and second where the ID doesn't match but the name does.
You can then use this table to join the two tables, retrieving up to one record from table 2 for each record in table 1.
I think you want something like this:
select t1.*,t2.*
from t1,t2
where t1.id = t2.id
and t1.name = t2.name
union
select t1.*,t2.*
from t1,t2
where t1.id ! t2.id
Your query should work, if the columns are coming from the right tables. Because you are not using table aliases, I suspect that you have an expression such as:
fname1 = fname2
and both columns are in the same table. Or worse:
fname1 = fname1
which is essentially always TRUE (except when fname1 is not null).
Your query might work, but it will be inefficient in most databases, because they will use nested loop optimizations. Consider rewriting the query to be:
from table1 t1 full outer join
table2 byID
on t1.IdNumber = byID.IdNumber full outer join
table2 byName
on t1.fname = byName.fname and t1.lname = byName.lname and t1.idNumber <> byName.idNumber
This will require changing other clauses in your query, typically to something like:
coalesce(byId.column, byName.column) as Column