I have a data table like this. A more close depiction of data.
col0 | col1 | col2 | col3 | col4
======================================
milk | egg | juice | |
egg | juice | | |
bread | jam | juice | |
wheat | egg | juice | |
jam | juice | | |
I want to first search for Juice in all columns and fetch all rows where juice is found. Then I have to fetch the rows with most unique and complete data. In this case it is the first row, since it has the most unique and complete data set with no repeating words. So, from this data if I search for 'juice', then i want this result
milk | egg | juice |
bread | jam | juice |
wheat | egg | juice |
I have tried to GROUP BY
and DISTINCT
on all column combinations, but i couldn't get desired result.
I used this statement
SELECT * FROM `table`
WHERE `col4` = 'juice' OR`col3` = 'juice' OR `col2` = 'juice' OR `col1` = 'juice'
group by `col2`,`col1`,`col0`;
I would prefer it to be in one statement instead of separate ones too.