trying to solve this with my limited knowledge of SQL and I'm stuck. :)
This is being done in SQLite 3 and Python 3.
I've 'JOIN'ed 2 tables (names and data).
I'm trying to search 2 columns in the result for user supplied text (case insensitive). If a match is found in either column, then return the row.
The problem I have, is that if the text is found in the name columns, I get multiple rows returned (I guess because there are multiple rows with the name name).
The required data is the 'name_id'
Here's a mockup of the join (there are other columns I haven't included here):
-------------------------------------------------------------------
| name_id | name | data_id | data |
-------------------------------------------------------------------
| 100 | John Smith | 200 | grey hair |
| 100 | John Smith | 201 | hairy teeth |
| 101 | Jerry Jones | 202 | white teeth |
| 103 | Barry Johnson | 256 | brown hair |
-------------------------------------------------------------------
So, if I search for "teeth", I get:
| 100 | John Smith | 201 | hairy teeth |
| 101 | Jerry Jones | 202 | white teeth |
and, if I search for "hair", I get:
| 100 | John Smith | 200 | grey hair |
| 100 | John Smith | 201 | hairy teeth |
| 103 | Barry Johnson | 256 | brown hair |
or, if I search for "john", I get:
| 100 | John Smith | 200 | grey hair |
| 100 | John Smith | 201 | hairy teeth |
| 103 | Barry Johnson | 256 | brown hair |
But what I actually want is only one row returned based on each 'name_id' so:
| 100 | John Smith | 200 | grey hair |
| 103 | Barry Johnson | 256 | brown hair |
The remaining SQLite command looks like this:
WHERE data LIKE "%john%" OR name LIKE "%john%" COLLATE NOCASE
I've tried using 'DISTINCT' but it appears to remove duplicate rows based upon the specified column. These missing rows contain data that is then not searched.
Thanks.
name_idyou could useGROUP BY name_idBut I believe the order in which the results are returned is determined by the order of theWHEREclause.