Skip to content

FIND not work with Inner Join

Problem:

I tried to use set_query_filter() to filter results. The code attached below:

$sql = 'SELECT meetings.c, meetings.id AS id, clients.name,

meetings.user_id, meetings.author_id, meetings.adress, meetings.start,
meetings.tocall, meetings.updated_at, meetings.created_at, clients.comment
FROM meetings INNER JOIN clients ON meetings.client_id = clients.id INNER
JOIN users ON meetings.user_id = users.id';

$dg = new \phpCtrl\C_DataGrid($sql, "id", "meetings");
$dg->set_query_filter('author_id = 100');

It returns an error

PHPGRID_ERROR: Column 'author_id' in where clause is ambiguous

Solution:


In your complex query below it joins a few tables, namely "meetings, "clients", and "users". 

SELECT meetings.c, meetings.id AS id, clients.name,
meetings.user_id, meetings.author_id, meetings.adress, meetings.start,
meetings.tocall, meetings.updated_at, meetings.created_at, clients.comment
FROM meetings INNER JOIN clients ON meetings.client_id = clients.id INNER
JOIN users ON meetings.user_id = users.id';

In the SQL error about clause being ambiguous, and I would assume you have passed author_id that is in more than one of the tables listed above, hence the it is 'ambiguous1. 

Error is: PHPGRID_ERROR: Column 'author_id' in where clause is ambiguous

The fix is to include table name before column name in the filter e.g. meetings.author_id. For example, 

 $dg->set_query_filter('meetings.author_id = 100');

Learn about ambiguous column name in this discussion.

Hope this helps! Happy gridding!

Feedback and Knowledge Base