Jump to content

Searching with relational databases


NiTx
Go to solution Solved by 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
Share on other sites

  • Solution

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)";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.