【问题标题】:mysql join getting a value or a null into a columnmysql join 在列中获取值或空值
【发布时间】:2012-01-04 05:37:16
【问题描述】:

我对 SQL 很陌生,所以我在搞清楚事情时遇到了一些问题。该数据库用于在线商店,结构如下:

用户:用户 ID、用户名等

价格表:PricelistID、UserID、ProductItemID、Price

productitems:ProductItemID、ProductID、ItemID

产品:ProductID、ManufacturerID、WebPageText 等

每个产品可以有一件或多件商品(例如,如果产品页面销售 T 恤,则可能有 5 种不同的 T 恤可供出售)。这很正常。不正常的是,每个项目的定价由用户的价格表中的内容决定,因为每个用户都被分配了特定的定价。并非所有用户都为每个项目分配了价格。

问题是:管理层的要求是用户应该能够看到每个产品,即使该产品不在用户的价目表中。如果用户没有产品项目的价目表条目,则页面应显示“联系我们进行定价”。我无法通过一个查询弄清楚如何做到这一点。我尝试的每个涉及价目表的连接都会抛出用户没有指定价格的产品。我只得到 ProductID 和 UserID。我把我的代码放在下面,它是 mysql 和 PHP 的混合体。它可以工作,但是很笨重,尤其是当我必须一遍又一遍地重做第二个查询时。请告诉我我应该怎么做。

$query_product = sprintf("SELECT * , (items.Stock - (SELECT Coalesce(Sum(DetailQuantity),0) 
FROM orderdetails 
INNER JOIN orders ON OrderID = DetailOrderID 
INNER JOIN productitems ON orderdetails.ProductItemID = productitems.ProductItemID
INNER JOIN products ON productitems.ProductID = products.ProductID
INNER JOIN items ON productitems.ItemID = items.ItemID
WHERE OrderDate > ProductUpdateDate))*ProductLive 
AS NumLeft
 FROM productitems
 INNER JOIN products ON products.ProductID = productitems.ProductID
JOIN items ON productitems.ItemID = items.ItemID
WHERE products.ProductID = %s",GetSQLValueString($ProductID, "int"));
$product = mysql_query($query_products, $x) or die(mysql_error());
$row_product = mysql_fetch_assoc($product);

//after narrowing down to one productitem, either through user selections or while looping through $row_product:

$query_price = sprintf("SELECT Price FROM pricelists 
WHERE UserID = %s AND ProductItemID = %s", GetSQLValueString($UserID, "int"), GetSQLValueString($row_product['ProductItemID'], 'int'));
$price = mysql_query($query_price, $x) or die(mysql_error());
$row_price = mysql_fetch_assoc($price);
$row_product['Price'] = $row_price['Price'];

在代码的后面,我检查 $row_products['Price'] 是否为空,以确定页面上显示的内容。

编辑:我认为这很明显,但是...我必须将价格表限制在 WHERE UserID = %s. 这意味着我不能只添加 LEFT JOIN pricelists ON pricelists.ProductItemID = productitems.ProductItemID,这实际上是我尝试的第一件事,并且不起作用。在 WHERE 语句中限制外连接会将其变为内连接。

【问题讨论】:

    标签: php mysql join calculated-columns


    【解决方案1】:

    将 pricelists 表作为左连接添加到您的主查询:

    SELECT field1, field2, ..., pl.*
    FROM orderdetails 
    INNER JOIN orders ON OrderID = DetailOrderID 
    ...
    LEFT JOIN pricelists pl PN pl.ProductItemID = productitems.ProductItemID
    ..
    

    在您的代码中检查 pricelists 表中的价格是否为空,如果是,则显示文本。

    【讨论】:

    • 外连接不起作用,因为我必须有 WHERE pricelists.UserID = %s 语句,它会自动将外连接转回内连接。
    • 其实,我找到了一个页面here,也许可以完成你的答案。这行得通吗? LEFT JOIN pricelists pl ON pl.ProductItemID = productitems.ProductItemID AND pl.UserID = %s
    • 将 AND 放入左连接有效。感谢您帮助我想出答案。
    【解决方案2】:

    您应该考虑为此使用外连接。这是对内连接和外连接之间区别的一个很好的描述 -> inner join vs outer join

    【讨论】:

    • 外连接不起作用,因为我必须有 WHERE pricelists.UserID = %s 语句,它会自动将外连接转回内连接。
    • 嗯?然后在您的联接中切换表的顺序或使用右外联接
    • @user1053109 我的意思是将您的 where 子句移动到连接的 ON 部分,如果需要重新排列您的连接。 -1 不知道如何加入。
    • 再读一遍。它解释了 on 与 where 子句的区别及其对连接的影响。
    • 我会让你变得非常简单。您确实注意到了这种语法,您没有:select a.*,b.* from a,b where a.a = b.b(+); vs select a.*,b.* from a,b where a.a = b.b; 如果您阅读了链接,它会表明您所做的实际上是一个内部连接,如果您想使用该语法,您需要使用 (+) 运算符或将条件移动到 ON 子句,否则它是 INNER JOIN。它甚至在最高响应中。查看 INNER JOIN 示例,然后查看 OUTER JOIN 示例,它应该很容易解释。
    猜你喜欢
    • 2019-04-23
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 2022-08-23
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多