gpt4 book ai didi

php - SQL Join 的 NULL 输出

转载 作者:行者123 更新时间:2023-11-29 23:09:17 26 4
gpt4 key购买 nike

我的问题是我收到了 NULL 输出。我的查询有什么问题?

$pr_code = $mysqli->real_escape_string($_POST['pr_code']);
$counter = $mysqli->real_escape_string($_POST['counter']);

$sql = $mysqli->query("
SELECT
a.*,
b.counter, b.pr pr_b
FROM
(SELECT *
FROM pr_list) a
LEFT JOIN
(SELECT counter, pr
FROM purchase_request) b
ON a.pr= b.pr
WHERE a.pr='$pr_code' AND b.counter='$counter'
");

while($row = $sql->fetch_assoc())
{
$pr= $row['pr'];
$approved= $row['approved'];
$requested = $row['requested'];
}

最佳答案

这不会为purchase_request值提供空值。

左连接明确允许 b 为空。所以我们使用内连接:

$sql = $mysqli->query("
SELECT
a.*,
b.counter, b.pr pr_b
FROM pr_list a
JOIN purchase_request b
ON a.pr= b.pr
WHERE a.pr='$pr_code' AND b.counter='$counter'
");

关于php - SQL Join 的 NULL 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28146412/

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