Jump to content

Searching with relational databases


NiTx

Recommended Posts

I have two tables, brands and devices.

 

brands table is set up like so:

 

id | brand_name 

1    Apple

2    Samsung

 

devices:

 

id | brand_id | dev_model | dev_version

1    1               iPhone         5s

2    2               Galaxy         S4

 

Right now I can search for a phone by its device name, for example: 'iPhone 5s', but I need my users to be able to search for the brand too: 'Apple iPhone 5s'. Is there a practical approach to doing this without having to add the brand names in my device table?

 

This is my current MySQL query: 

$sql = "(SELECT id, brand_id, dev_model, dev_version FROM devices WHERE ";
$sql .= "CONCAT(dev_model, ' ', dev_version) LIKE '%{$database->escape_values($term)}%' ";
$sql .= "LIMIT 4)";

Any help would be appreciated, or even a point in the right direction.

 

Link to comment
https://forums.phpfreaks.com/topic/282878-searching-with-relational-databases/
Share on other sites

Solved the issue. Discovered LEFT JOIN. As with most of my php problems, I just don't know what to search in Google to find the answers to my issue.. For anyone else with the same issue this is how I solved my issue.

$sql = "(SELECT devices.id, devices.brand_id, devices.dev_model, devices.dev_version, devices.dev_model_number, devices.dev_release_month, devices.dev_release_year, brands.brand_name FROM devices  ";
$sql .= "LEFT JOIN brands  ON devices.brand_id = brands.id ";
$sql .= "WHERE CONCAT(brands.brand_name ,' ', devices.dev_model, ' ', devices.dev_version, ' ', devices.dev_model_number) LIKE '%{$database->escape_values($term)}%' ";
$sql .= "ORDER BY devices.dev_release_year DESC, devices.dev_release_month DESC LIMIT 4)";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.