José Romero
José Romero

Reputation: 124

How to join tables on varchar fields in MySQL

Table_1
type   |   description
Error      Some description
Success    Another description

Table_2
type   |   description
Error 1    Some description
Error 2    Another description
Error 3    Yet another description

I need to join these two tables by the type field, which is a varchar data type in both tables. The problem is that since "Error" is not the same as "Error 1" or "Error 2", when I compare both fields, it returns empty results. I've tried:

select * from Table_1 a
left join Table_2 using (type)

select * from Table_2
where type in (select distinct type from Table_1)

Any help would be highly appreciated, thanks in advance

EDIT: I should be able to get results when a type in Table_1 is contained in a type in Table_2. I know it's a little hard to get, but the thing is that in Table_1 I have generic errors for different scenarios, and Table_2 contains these same errors, but with a little more information next to them. Table_2 is filled up with data that comes from log files, and there's pretty much anything I can do about it.

Upvotes: 4

Views: 9880

Answers (1)

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107536

Your joins should be fine. A third way:

select * from Table_1 a
left join Table_2 b on a.type = b.type

If you're not getting any results then the type columns' values are not equal.

Update

Given that your comment states that Table_1.type is a substring of Table_2.type, you can change the join operator:

select * from Table_1 a
left join Table_2 b on b.type LIKE '%' + a.type + '%'

This practice is not ideal. Use with caution.

Upvotes: 5

Related Questions