[Solved] LEFT JOIN return result for not equal ON

EverSQL Database Performance Knowledge Base

LEFT JOIN return result for not equal ON

Database type:

I have this mysql query

SELECT bm_events.name AS event_name,
       bm_events.uid AS event_uid,
       bm_events.organising,
       bm_organising_company.uid,
       bm_organising_company.name
FROM bm_events LEFT JOIN bm_organising_company
     ON bm_events.organising = bm_organising_company.uid 

And here is the result

Acontraluz  68  66,90   66  Advanced Music S.L.

Column "bm_events.organising" is a varchar with comma separated values.

So as "bm_events.organising" is equal to "66,90" on bm_events table, I expect to have no result for bm_organising_company.uid, bm_organising_company.name.

Could you explain to me why I got the result of bm_organising_company.uid = 66 ?

Thanks :)

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `bm_organising_company` ADD INDEX `bm_company_idx_uid` (`uid`);
The optimized query:
SELECT
        bm_events.name AS event_name,
        bm_events.uid AS event_uid,
        bm_events.organising,
        bm_organising_company.uid,
        bm_organising_company.name 
    FROM
        bm_events 
    LEFT JOIN
        bm_organising_company 
            ON bm_events.organising = bm_organising_company.uid

Related Articles



* original question posted on StackOverflow here.