Medoo InnerJoin query doesn't work on MSSQL database

125 Views Asked by At

I am using MSSQL as my database and Medoo isn't successfully performing select queries with an INNER JOIN.

This is my query.

$configurations = $this->database->select("application_configurations", [
  "[><]environments" => ["active_environment_id", "id"]
  ], [
    "environments.name"
  ], ["application_configurations.application_id" => $id]);

After looking at the query logs, this is what I found was being outputted:

'SELECT [environments].[name] FROM [application_configurations] INNER JOIN [environments] USING ([active_environment_id], [id]) WHERE [application_configurations].[application_id] = 1'

MSSQL doesn't like this :

""active_environment_id" is not a recognized table hints option."

I would have written this :

select environments.name from application_configurations
inner join environments on environments.id = application_configurations.active_environment_id
where application_configurations.application_id = 1

Very simple queries work

$configurations = $this->database->select("application_configurations",
  ["active_environment_id"],
  ["application_id" => $id]
);

How can I get this to work with MSSQL?

EDIT

I tried using the left join query as suggested in the comments.

It still did not work, this was the query done behind the scenes.

SELECT [environments].[name] FROM [application_configurations]
 LEFT JOIN [environments] USING ([active_environment_id], [id])
 WHERE [application_configurations].[application_id] = 1`
1

There are 1 best solutions below

0
PinothyJ On

It does work, you have just misunderstood the instructions for joins (which is pretty standard considering how brief the documentation on them is).

What you want is the following (for an inner join):

$database->select('application_configurations',
    [       //  join
        "[><]environments" => ["active_environment_id" => "id"],
    ], [    //  columns
        'environments.name'
    ], [    //  where
        'application_configurations.application_id' => $id
    ]
);

If that is also a view that your application is accessing often, you may want to consider making a view of it on your server. Calling a view on your server will be cheaper that a query any day.

I hope that is just what you need.