Vigor
Vigor

Reputation: 1754

Mysql Join two tables on Like with Concat

I want to join two tables on a like clause. My schema is as the link:

Sql Fiddle 1

As you can see the result is empty.

But if I use like as follows:

Sql Fiddle 2

As you see I can get the results with name or des contains 'GRE'. So what's the problem here?

I searched the answer for a while, and found the suggested way to do this is the same as I did:

similar question

Any suggestions will be highly appreciated.

Upvotes: 1

Views: 271

Answers (1)

Aarif Aslam
Aarif Aslam

Reputation: 1123

You are doing it correctly. you just have to use TRIM() to remove trailing whitespaces. Use it like

SELECT
    a.id as app_id,a.app_name,a.des,
    b.id as tag_id, b.name as tag_name
FROM aa_t_aaaa_app a
JOIN aa_t_aaaa_tag b
ON ((a.app_name LIKE CONCAT('%', TRIM(b.name) ,'%')) or (a.des Like CONCAT('%', TRIM(b.name) ,'%')))
order by a.id`

Upvotes: 6

Related Questions