Context:
The database is about phones. I have a search engine in a TABLE sav
(records of "after sales service") which has a field phone
containing the IMEI (its unique id) of the phone.
In some case, the phone is going back in our stock (deleted from the TABLE sales
and inserted into the TABLE stock
) and an other phone is returned to the customer.
Issue:
In the search engine of the "after sales service", I use to LEFT JOIN sales
, but in case a phone has gone back in stock
, it won't be found by its IMEI.
To sum:
I need to join a table if a record is found, else, join an other table.
My actual query:
I shortened it ([...]
) to make it readable:
SELECT sav.id, sav.phone, [...] FROM sav
LEFT JOIN sales ON (sav.phone = sales.imei)
LEFT JOIN stock_model ON (sales.model = stock_model.id) [...]
WHERE (sales.imei LIKE :search_1 OR [...])
OR (sales.imei LIKE :search_2 OR [...])
OR [...]
I need to change the 2nd line but I don't understand how. The questions I read before seem to answer specific cases.
EDIT: table structure & dummy data
I won't post irrelevant fields (too many)
TABLE
sav
| id | phone | issue | replacement_imei | ...
----------------------------------------------------
| 35 | 123456789 | battery | NULL | ... <-- phone n°1
| 54 | 987654321 | screen | 147258369 | ... <-- phone n°2 replaced by phone n°3
TABLE
stock
| id | imei | model | color | memory | ...
-------------------------------------------------
| 68 | 369258147 | 2 | 4 | 1 | ...
| 92 | 987654321 | 5 | 8 | 3 | ... <-- phone n°2 replaced by phone n°3
TABLE
sales
| id | imei | model | color | memory | ...
-------------------------------------------------
| 68 | 123456789 | 2 | 4 | 1 | ... <-- phone n°1
| 103 | 147258369 | 5 | 8 | 3 | ... <-- phone n°3 replaced phone n°2
EDIT: complete search engine [PHP]:
In case the whole function helps:
$keywords = explode(" ", $keywords);
$query = "SELECT sav.id, sav.phone, sav.date, sav.nom, sav.issue, sav.replacement_imei, sav.commentaire, sav.attente, sav.pieces, sav.frais, sav.date_envoi, sav.tracking, sav.termine FROM sav LEFT JOIN sales ON (sav.phone = sales.imei) LEFT JOIN stock_memory ON (sales.memory = stock_memory.id) LEFT JOIN stock_color ON (sales.color = stock_color.id) LEFT JOIN stock_model ON (sales.model = stock_model.id) LEFT JOIN stock_grade ON (sales.grade = stock_grade.id) WHERE ";
// Makes a long query returning each row where at least 1 keyword matches 1 column
$query_array = array();
for ($i = 0; $i < count($keywords); $i += 1) {
$query .= " ( sales.imei LIKE :search_" . $i;
$query .= " OR ( sales.snowden = 1 AND 'snowden'= :search_" . $i . " )";
$query .= " OR stock_model.name LIKE :search_" . $i;
$query .= " OR stock_color.name LIKE :search_" . $i;
$query .= " OR stock_memory.amount LIKE :search_" . $i;
$query .= " OR stock_grade.name LIKE :search_" . $i;
$query .= " OR sav.id LIKE :search_" . $i;
$query .= " OR DATE_FORMAT(sav.date, '%d/%m/%Y') LIKE :search_" . $i;
$query .= " OR sav.nom LIKE :search_" . $i;
$query .= " OR sav.issue LIKE :search_" . $i;
$query .= " OR sav.replacement_imei LIKE :search_" . $i;
$query .= " OR sav.commentaire LIKE :search_" . $i;
$query .= " OR sav.attente LIKE :search_" . $i;
$query .= " OR sav.pieces LIKE :search_" . $i;
$query .= " OR sav.frais LIKE :search_" . $i;
$query .= " OR DATE_FORMAT(sav.date_envoi, '%d/%m/%Y') LIKE :search_" . $i;
$query .= " OR sav.tracking LIKE :search_" . $i;
$query .= " OR ( sav.termine = 1 AND 'termine'= :search_" . $i . " )";
// $query .= "LIKE :search_" . $i;
if ($i != (count($keywords) - 1)) {
$query .= " ) AND ";
} else {
$query .= " ) ";
}
if (strtolower($keywords[$i]) == 'snowden' || strtolower($keywords[$i]) == 'termine') {
$query_array['search_' . $i] = $keywords[$i];
} else {
$query_array['search_' . $i] = "%" . $keywords[$i] . "%";
}
}
$query .= "ORDER BY sav.id DESC LIMIT $offset, $limit";
$results = $this->bdd->prepare($query);
$results->execute($query_array);
return $results;