我正在尝试加入上传的CSV(将MySQL0连接到多个其他SQL表并创建输出,但是我遇到了一个问题,我从第一个表(上传的CSV)中看到了我的数据,但它与之不匹配其他表(现有的MySQL表)。我检查以确保我LEFT JOIN所依据的列的格式是相同的。是我在最后的JOIN中使用OR语句的问题,还是可能是陈述是基本的模糊逻辑。
$sql= "SELECT x.*, y.*, z.* FROM tbl_invoice as x LEFT JOIN conversion as y ON
y.doc_sku = x.doc_sku LEFT JOIN tbl_catalog as z ON z.distributor_sku = COALESCE(y.dist_sku, x.doc_sku) OR x.item_name like '%z.item_description%' WHERE x.timestamp IN (SELECT MAX(timestamp) FROM tbl_invoice)";
//echo $sql;die;
$result = $conn->query($sql);
$res = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$res[] = $row;
}
}
$html = "<table><tr><th>manufacturer</th><th>item_name</th><th>item_quantity</th><th>purchase_price</th><th>purchase_quantity</th><th>dist_sku</th><th>Total</th><th>item_description</th></tr>";
foreach($res as $key=>$val)
{
$html .="<tr><td>".$val['manufacturer']."</td><td>".$val['item_name']."</td><td>".$val['item_quantity']."</td><td>".$val['purchase_price']."</td><td>".$val['purchase_quantity']."</td><td>".$val['dist_sku']."</td><td>".($val['purchase_price']-$val['price'])*$val['purchase_quantity']."</td><td>".$val['item_description']."</td></tr>";
}
$html .="</table>";
echo $html;
unlink($inputFileName);
echo "<pre>";print_r($res);die;
} else {
echo "Sorry, there was an error uploading your file.";
}}?>
答案 0 :(得分:1)
您的加入like
声明无法按预期运行。它正在寻找字符串z.item_description
的文字副本。您的查询应该是
SELECT
x.*
, y.*
, z.*
FROM
tbl_invoice as x
LEFT JOIN conversion as y
ON y.doc_sku = x.doc_sku
LEFT JOIN tbl_catalog as z
ON z.distributor_sku = COALESCE(y.dist_sku, x.doc_sku)
OR x.item_name like concat('%', z.item_description, '%')
WHERE
x.timestamp IN (
SELECT
MAX(timestamp)
FROM
tbl_invoice
)