gpt4 book ai didi

php - MySQL JOIN 一个表或另一个表

转载 作者:行者123 更新时间:2023-11-29 18:00:08 24 4
gpt4 key购买 nike

上下文:
该数据库是关于电话的。我在 TABLE sav(“售后服务”记录)中有一个搜索引擎,其中的 phone 字段包含手机的IMEI(其唯一 ID)。
在某些情况下,手机会退回我们的库存(从 TABLE sales 中删除并插入 TABLE stock 中),而另一部手机则会退还给客户。

问题:
在“售后服务”的搜索引擎中,我使用 LEFT JOIN sales ,但如果手机返回 stock ,则无法通过其 IMEI 找到它。
< br/>总和:

I need to join a table if a record is found, else, join an other table.

我的实际查询:
我缩短了它( [...] )以使其可读:

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 [...]

我需要更改第二行,但我不明白如何更改。我之前读过的问题似乎回答了具体情况。

编辑:表结构和虚拟数据
我不会发布不相关的字段(太多)
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

编辑:完整的搜索引擎[PHP]:
如果整个功能有帮助:

$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;

最佳答案

首先,我通过完整外部联接获取所有数据,然后 Sales.Model => 从 Stock_Model 选择所有 ID。

SELECT sav.id, sav.phone, [...] FROM sav 
FULL OUTER JOIN sales ON (sav.phone = sales.imei)
WHERE sav.phone = sales.imei or sales.model in (select Id from stock_model)

关于php - MySQL JOIN 一个表或另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48441488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com