We have a table with the car owners data. Some people may have nore than one car. Our task is to provide a UI that will allows to search by fields like car color or year of make but display a list of matching the search without duplicates.
For instance we want to display a list car owners with red cars. The correct query would be as follows:
select distinct name from table where property2='red'
The problem is that fields like property1, property2 etc need to be a part of the select query, otherwise we cannot select those fields to be searchable. Luckily, there is an easy way to implement this in PHPRunner.
- Leave the default SQL query intact:
SELECT
name,
property1,
property2,
property3,
property4
FROM
table
Make fields like property1, property2 etc searchable but remove them from the List page.
In BeforeSQLQuery event use the following code:
$strSQL = "select distinct `name` from `table`";
if($strWhereClause)
$strSQL = "select distinct `name` from `table` where ".$strWhereClause;
This is it. Happy coding!