I need to contruct a query, from keywords entered by a user in a text-field, which will do the following: Take the keywords and search in a table, after which car has all the given keywords, less than all and continuing down the car which has just one keyword.
My table looks like this:
field_id | car_id | keyword |
----------------------------
1 | 5 | 1989 |
-----------------------------
2 | 8 | old |
-----------------------------
3 | 8 | ford |
-----------------------------
6 | 8 | cheap |
-----------------------------
4 | 7 | ford |
-----------------------------
6 | 7 | cheap |
-----------------------------
5 | 6 | ford |
-----------------------------
Say if the user searched for “old”, “ford” and “cheap”, car_id 8, 7 and 6 should be returned because they contains the keywords. Furthermore I need to know which car has which keywords (8: “old”, “ford”, “cheap” And 7: “ford”, “cheap” And 6: “ford”) . Im not an expert with MySQL so I don’t now if it is possible to create some sort of multidimensional array here…
I have a query which select only the car which has all the keywords entered, if it is of any use:
Select car_id from keywords
Where keyword in ("old","ford", "cheap")
group by car_id having count(*) = 3;
You dont need having because it will only send whenever there exist only 3 cars belongs to given keywords. Try this
This will give you comma separated keywords that belongs to car