I'm working in PHP and MySQL
I feel like punching a wall. I have no idea how to do this and it's making me angry. I've done plenty of things like it before and this isn't working. UGH. Please help!!!!
I'm doing a search. Customer will type in something like "88 brake pads". I want to return all the parts that say "brake" all the parts that say "pads" and all the parts that have the year 88. My issue here is, the years and the parts are in different tables.
Example:
Years Table
ID | part_id | year |
============================
1 | 15 | 1945 |
2 | 15 | 1946 |
3 | 16 | 1984 |
4 | 18 | 1987 |
Parts Table
ID | part_name |
=====================
15 | brakes |
16 | hose |
17 | crank |
18 | muffler |
This SQL Works, when pulling out of parts table
SELECT DISTINCT * FROM hj_parts WHERE part_name LIKE "%brakes"
I will get the output
ID | part_name |
=====================
15 | brakes |
So what I need is if someone enters "brakes 87" I want to get back all the info for part 15 (brakes) and part 18 (because it's from 1987).
I am so totally lost.
I've done something like this to pull out all the parts from a certain year, and I thought I could just tweak it, and it's not helping.
$query = "SELECT hj_parts.* FROM hj_parts JOIN(SELECT DISTINCT part_id FROM hj_years WHERE (year BETWEEN $starter AND $ender) OR (year='all')) hj_years ON hj_parts.id = hj_years.part_id";
Here's how I'd do it. First create a query that brings everything together, and we'll put it in a view for convenience sake
create view AllParts as
SELECT Parts.*,Years.* from Parts inner join Years on Years.part_id=Parts.ID
Then you do a search like this
Select * from AllParts where part_name like '%brake' or year=1987
If your database is big this might take a while. It's not necessarily a perfect solution, but it can work for you.
If you are interested in doing a keyword search, perhaps a nosql database would be better